【问题标题】:how to get screen width through php? [duplicate]如何通过php获取屏幕宽度? [复制]
【发布时间】:2013-06-04 11:53:38
【问题描述】:

嗨,我知道 php 是服务器端的东西...但我想知道如何通过 php 获取用户屏幕宽度。 我在网上搜索,他们说我必须同时使用 javascript 和 php。 但我不知道如何工作。 请以最简单的方式告诉我通过 php 获取用户的屏幕宽度。 如果只能通过 javascript 完成,请告诉我如何将 javascript 的宽度变量以整数格式(不是字符串)传递给 php 变量。

请给我看代码...我真的搞砸了这一切... 谢谢:)

【问题讨论】:

  • 你需要什么宽度?无论如何,您所做的任何事情都应该在客户端完成。
  • 有问题的变量是javascript中的screen.width。使用图像像素或 ajax 发送此变量的方式取决于您。
  • 不要。只是不要。您可以通过 JavaScript 设置屏幕宽度。因此,您可以执行 Ajax 请求或从 Javascript 中设置一个包含屏幕宽度的 cookie,但它不可靠,并且仅在第一个页面查看后才有效,并且只有在启用 Javascript 并且有效的情况下才有效。最好使用CSS media queries
  • 已经有很多答案了,但是如果你说为什么你想要屏幕宽度,你可能会找到 CSS 的帮助,这就是你通常应该如何处理屏幕尺寸问题
  • 我需要屏幕宽度,以便我可以根据屏幕大小更改“添加此按钮”的位置。

标签: php ajax screen-resolution


【解决方案1】:

我不确定,但它可能对你有帮助

    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script>
    var width = $(window).width();
    //This is ajax code which will send width to your php page in the screenWidth variable
    $.ajax({
    url: "example.php", //this will be your php page 
    data : {screenwidth:width} 
    }).done(function() {
    $(this).addClass("done");
    });
    </script>

    //example.php code is here :
    <?php
    echo $width = $_REQUEST['screenwidth'];
    ?>

【讨论】:

    【解决方案2】:

    不可能使用完整的php,您可能还需要使用javascript 无论如何试试这个

    <?php
    session_start();
    if(isset($_SESSION['screen_width']) AND isset($_SESSION['screen_height'])){
        echo 'User resolution: ' . $_SESSION['screen_width'] . 'x' . $_SESSION['screen_height'];
    } else if(isset($_REQUEST['width']) AND isset($_REQUEST['height'])) {
        $_SESSION['screen_width'] = $_REQUEST['width'];
        $_SESSION['screen_height'] = $_REQUEST['height'];
        header('Location: ' . $_SERVER['PHP_SELF']);
    } else {
        echo '<script type="text/javascript">window.location = "' . $_SERVER['PHP_SELF'] . '?width="+screen.width+"&height="+screen.height;</script>';
    }
    ?>
    

    【讨论】:

    • 它正在工作:D 谢谢,但请你解释一下
    • 天哪,这确实有效!我能找到的网络和stackoverflow上唯一可以将屏幕宽度和高度放入变量的来源。天才。 (可能无法在 IE 上工作,但当然可以;p)
    【解决方案3】:

    我同意依赖客户端的屏幕宽度是一个坏主意,但在这里,为了您的启迪:

    javascript:

        if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.open("GET","set_screen_width.php?s="+screen.width,false);
    xmlhttp.send();
    

    如果您在加载页面时执行此脚本,它会检查屏幕宽度并将其发送到您的 php 文件,该文件需要类似于:

        <?PHP
    session_start();
    $_SESSION['screen']['width']=$_GET['s'];
    ?>
    

    所有其他 PHP 页面都可以访问 $_SESSION 变量 vis:

    <?PHP
    session_start();
    $screen_width=$_SESSION['screen']['width'];
    

    等等……

    最好不要尝试发送仅在一个屏幕宽度上看起来不错的 HTML。你可以,但这是一场失败的游戏。你只是不知道客户会用它做什么。例如,假设他们视力低下,喜欢使用较大的字体。

    【讨论】:

      猜你喜欢
      • 2011-09-25
      • 2015-02-22
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      • 2013-10-11
      • 2012-12-17
      • 2017-10-01
      • 2012-06-02
      相关资源
      最近更新 更多