9

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

  • Here's a [**fully working example**](https://stackoverflow.com/a/55933429/8112776) that uses a hidden form to `POST` the data back to PHP. – ashleedawg May 01 '19 at 08:24

8 Answers8

25

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.

PaulJWilliams
  • 18,187
  • 3
  • 48
  • 78
5

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?

Dave Sherohman
  • 43,013
  • 12
  • 61
  • 98
  • 2
    +1. I have 2560x1600 screen resolution, I almost never run maximised. You should be reading the view port size (window area not counting toolbars, scrollbars, status bar and window decorations). – SpliFF May 21 '09 at 10:06
4

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>");
brasofilo
  • 23,940
  • 15
  • 86
  • 168
schnaader
  • 46,785
  • 9
  • 98
  • 132
2

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.

EFC
  • 1,650
  • 15
  • 34
  • You know the resolution at one point in time.. but by the time you load after the redirect, who knows what the resolution is. – xaxxon Dec 11 '12 at 03:59
2

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>
TheBlackBenzKid
  • 24,177
  • 36
  • 128
  • 199
1

Some people require browser size for mobile devoloping. This is essential information in some cases.

Using WURFL and WALL can get around this as most mobiles do not support JS.

Paul
  • 11
  • 1
0

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

Thinker
  • 12,902
  • 8
  • 37
  • 52
  • 3
    Lloyd: you can detect the screen resolution, but not the monitor size. A 1600x1200 resolution can mean an old 21" CRT, a moderately big LCD, and a really beautifull and dense smaller screen around 16" – Csaba Kétszeri May 21 '09 at 11:45
  • monitor size cannot be detected, only window resolution. – xaxxon Dec 11 '12 at 03:55
0

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.

Jet
  • 1,141
  • 6
  • 8