Getting the screen resolution using PHP

前端 未结 21 1895
你的背包
你的背包 2020-11-22 06:50

I need to find the screen resolution of a users screen who visits my website?

相关标签:
21条回答
  • 2020-11-22 07:18

    You can try RESS (RESponsive design + Server side components), see this tutorial:

    http://www.lukew.com/ff/entry.asp?1392

    0 讨论(0)
  • 2020-11-22 07:18

    PHP works only on server side, not on user host. Use JavaScript or jQuery to get this info and send via AJAX or URL (?x=1024&y=640).

    0 讨论(0)
  • 2020-11-22 07:21

    I don't think you can detect the screen size purely with PHP but you can detect the user-agent..

    <?php
        if ( stristr($ua, "Mobile" )) {
            $DEVICE_TYPE="MOBILE";
        }
    
        if (isset($DEVICE_TYPE) and $DEVICE_TYPE=="MOBILE") {
            echo '<link rel="stylesheet" href="/css/mobile.css" />'
        }
    ?>
    

    Here's a link to a more detailed script: PHP Mobile Detect

    0 讨论(0)
  • 2020-11-22 07:22

    Directly with PHP is not possible but...

    I write this simple code to save screen resolution on a PHP session to use on a image gallery.

    <?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>';
    }
    ?>
    
    0 讨论(0)
  • 2020-11-22 07:24

    You can set window width in cookies using JS in front end and you can get it in PHP:

    <script type="text/javascript">
       document.cookie = 'window_width='+window.innerWidth+'; expires=Fri, 3 Aug 2901 20:47:11 UTC; path=/';
    </script>
    
    <?PHP
        $_COOKIE['window_width'];
    ?>
    
    0 讨论(0)
  • 2020-11-22 07:25

    PHP is a server side language - it's executed on the server only, and the resultant program output is sent to the client. As such, there's no "client screen" information available.

    That said, you can have the client tell you what their screen resolution is via JavaScript. Write a small scriptlet to send you screen.width and screen.height - possibly via AJAX, or more likely with an initial "jump page" that finds it, then redirects to http://example.net/index.php?size=AxB

    Though speaking as a user, I'd much prefer you to design a site to fluidly handle any screen resolution. I browse in different sized windows, mostly not maximized.

    0 讨论(0)
提交回复
热议问题