can php detect client browser monitor size/resolution?

前端 未结 8 1131
一生所求
一生所求 2020-12-20 16:20

Php can detect IP, hostname, client agent etc. Can php detect client browser monitor size/resolution?

相关标签:
8条回答
  • 2020-12-20 16:43

    Monitor size can't be obtained using JS, you have to make a poll :)

    0 讨论(0)
  • 2020-12-20 16:44

    Please do note that some of us like our browsers non-maximized. Perhaps you'd be better off trying to detect browser size rather than screen resolution. I assume that the JS to do either would be very similar, but I don't actually know that to be the case.

    Also, what is the resolution of a blind man's screen reader?

    0 讨论(0)
  • 2020-12-20 16:44

    You'll have to use PHP together with JavaScript, like in this example:

    $url = $_SERVER['PHP_SELF'];
    
    if( isset($HTTP_COOKIE_VARS["res"]) )
        $res = $HTTP_COOKIE_VARS["res"];
    
    else {
        ?>
        <script language="javascript">
        <!--
        go();
    
        function go() 
        {
            var today = new Date();
            var the_date = new Date("August 31, 2020");
            var the_cookie_date = the_date.toGMTString();
            var the_cookie = "res="+ screen.width +"x"+ screen.height;
            var the_cookie = the_cookie + ";expires=" + the_cookie_date;
            document.cookie=the_cookie
                location = '<?echo "$url";?>';
        }
        //-->
        </script>
        <?php
    }
    
    //Let's "split" the resolution results into two variables
    list($width, $height) = split('[x]', $res);
    
    //Take the width and height minus 300
    $tb_width = $width-300;
    $tb_height = $height-300;
    
    //Make the table
    print("<table align=center border=1 width=" .$tb_width . " height=" . $tb_height . " >
        <tr><td align=center>Your screen resolution is " . $width . " by " . $height . ".<br>
        The width/height of this table is " . $tb_width . " by " . $tb_height . ".</td></tr>
        </table>");
    
    0 讨论(0)
  • 2020-12-20 16:46

    I was looking for this as well, but found none of these answers really answered the question! Indeed, there is no way for PHP to know the screen resolution since it is running on the server side. Since that information is not passed along in HTTP environment variables, we need another route. Javascript is one alternative.

    The example below is a PHP page that checks for a resolution variable being passed in the HTTP request. If it does not find that resolution variable, then it creates a brief bit of JavaScript on the page that passes that variable and the height and width along in a redirect back to itself. Of course, when the page is loaded again after the redirect all the variables will be set and PHP will know the resolution.

    <?php
        if(!isset($_GET['resolution'])) {     
            echo "<script language=\"JavaScript\">     
    <!--      
        document.location=\"$PHP_SELF?resolution=1&width=\"+screen.width+\"&height=\"+screen.height;     
    //-->     
    </script>";     
        } else {
            // Code to be displayed if resolution is detected     
            if(isset($_GET['width']) && isset($_GET['height'])) {     
                echo "Width: " . $_GET['width'] . " and Height: " . $_GET['height'] . "<br />";
            } else {     
                echo "Resolution not detected.";
            }     
        }
    ?>
    

    In the end I found this a pretty unsatisfactory solution. It works, but it is ugly, adding cruft to the URL and requiring a redirect. Still, it may inspire someone to post a better answer. FYI, credit where credit is due, this answer was inspired by this post.

    0 讨论(0)
  • 2020-12-20 16:46

    I know this is not the best answer so spare the downvote.

    <script>
    /*
        JAVASCRIPT IS ON TELL THE DEVELOPER#
    */
    // GET BROWSER WIDTH AND HEIGHT
    var bh=screen.height;
    var bw=screen.width;
    window.location="?doSubmit=do&js=yes&bh="+bh+"&bw="+bw+"";
    </script>
    
    <noscript>
        <!--
            JAVASCRIPT IS OFF TELL THE DEVELOPER#
        -->
        <meta http-equiv='refresh' content='0;url=?doSubmit=do&js=off&bh=off&bw=off'>
    
    </noscript>
    
    <?
    
    if($_GET["doSubmit"]=="do"){
    
        // VARS
    
            $bh=$_GET["bh"];
            $bw=$_GET["bw"];
            $js=$_GET["js"];
    
        // PRINT HTML ?>
    
    <table>
    
        <tr><td><strong>Browser Width:</strong></td><td><?=$bw;?>px</tr>
        <tr><td><strong>Browser Height:</strong></td><td><?=$bh;?>px</tr>
        <tr><td><strong>JavaScript Detection (y/n):</strong></td><td><?=$js;?></tr>
    
    </table>
    
    0 讨论(0)
  • 2020-12-20 16:47

    No, it cant. PHP runs on the server, so it cant detect client settings unless you take specific client-side steps to pass the info to the PHP scripts on the server.

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