can php detect client browser monitor size/resolut

2020-02-13 08:54发布

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

8条回答
Deceive 欺骗
2楼-- · 2020-02-13 09:16

Note, that JS can check the window size of browser, but this size includes user toolbars, scrollbars etc... Real workspase area in browser depends on those toolbars size.

查看更多
倾城 Initia
3楼-- · 2020-02-13 09:19

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.

查看更多
倾城 Initia
4楼-- · 2020-02-13 09:20

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>
查看更多
在下西门庆
5楼-- · 2020-02-13 09:25

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

查看更多
放我归山
6楼-- · 2020-02-13 09:26

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>");
查看更多
Emotional °昔
7楼-- · 2020-02-13 09:26

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.

查看更多
登录 后发表回答