Getting the screen resolution using PHP

前端 未结 21 1894
你的背包
你的背包 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:05

    JS:

    $.ajax({
        url: "ajax.php",
        type: "POST",
        data: "width=" + $("body").width(),
        success: function(msg) {
    
            return true;
        }
    });
    

    ajax.php

    if(!empty($_POST['width']))
        $width = (int)$_POST['width'];
    
    0 讨论(0)
  • 2020-11-22 07:05

    The quick answer is no, then you are probably asking why can't I do that with php. OK here is a longer answer. PHP is a serverside scripting language and therefor has nothing to do with the type of a specific client. Then you might ask "why can I then get the browser agent from php?", thats because that information is sent with the initial HTTP headers upon request to the server. So if you want client information that's not sent with the HTTP header you must you a client scripting language like javascript.

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

    Here is the Javascript Code: (index.php)

    <script>
        var xhttp = new XMLHttpRequest();  
        xhttp.open("POST", "/sqldb.php", true);
        xhttp.send("screensize=",screen.width,screen.height);
    </script>
    

    Here is the PHP Code: (sqldb.php)

    $data = $_POST['screensize'];
    $pdo = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
    $statement = $pdo->prepare("UPDATE users SET screen= :screen WHERE id = $userid");
    $statement->execute(array('screen' => $data));
    

    I hope that you know how to get the $userid from the Session, and for that you need an Database with the Table called users, and an Table inside users called screen ;=) Regards KSP

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

    In PHP there is no standard way to get this information. However, it is possible if you are using a 3rd party solution. 51Degrees device detector for PHP has the properties you need:

    • $_51d['ScreenPixelsHeight']
    • $_51d['ScreenPixelsWidth']

    Gives you Width and Height of user's screen in pixels. In order to use these properties you need to download the detector from sourceforge. Then you need to include the following 2 lines in your file/files where it's necessary to detect screen height and width:

    <?php
    require_once 'path/to/core/51Degrees.php';
    require_once 'path/to/core/51Degrees_usage.php';
    ?>
    

    Where path/to/core is path to 'Core' directory which you downloaded from sourceforge. Finally, to use the properties:

    <?php
    echo $_51d['ScreenPixelsHeight']; //Output screen height.
    echo $_51d['ScreenPixelsWidth']; //Output screen width.
    ?>
    

    Keep in mind these variables can contain 'Unknown' value some times, when the device could not be identified.

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

    This can be done easily using cookies. This method allows the page to check the stored cookie values against the screen height and width (or browser view port height and width values), and if they are different it will reset the cookie and reload the page. The code needs to allow for user preferences. If persistant cookies are turned off, use a session cookie. If that doesn't work you have to go with a default setting.

    1. Javascript: Check if height & width cookie set
    2. Javascript: If set, check if screen.height & screen.width (or whatever you want) matches the current value of the cookie
    3. Javascript: If cookie not set or it does not match the current value, then: a. Javascript: create persistent or session cookie named (e.g.) 'shw' to value of current screen.height & screen.width.
      b. Javascript: redirect to SELF using window.location.reload(). When it reloads, it will skip the step 3.
    4. PHP: $_COOKIE['shw'] contains values.
    5. Continue with PHP

    E.g., I am using some common cookie functions found on the web. Make sure setCookie returns the correct values. I put this code immediately after the head tag. Obviously the function should be in a a source file.

    <head>
    <script src="/include/cookielib.js"></script>
    <script type=text/javascript>
    function setScreenHWCookie() {
        // Function to set persistant (default) or session cookie with screen ht & width
        // Returns true if cookie matches screen ht & width or if valid cookie created
        // Returns false if cannot create a cookies.
        var ok  = getCookie( "shw");
        var shw_value = screen.height+"px:"+screen.width+"px";
        if ( ! ok || ok != shw_value ) {
            var expires = 7 // days
            var ok = setCookie( "shw", shw_value, expires)
            if ( ok == "" ) {
                // not possible to set persistent cookie
                expires = 0
                ok = setCookie( "shw", shw_value, expires)
                if ( ok == "" ) return false // not possible to set session cookie
            }
            window.location.reload();
        }
        return true;
    }
    setScreenHWCookie();
    </script>
    ....
    <?php
    if( isset($_COOKIE["shw"])) {
        $hw_values = $_COOKIE["shw"];
    }
    
    0 讨论(0)
  • 2020-11-22 07:09

    Easiest way

    <?php 
    //-- you can modified it like you want
    
    echo $width = "<script>document.write(screen.width);</script>";
    echo $height = "<script>document.write(screen.height);</script>";
    
    ?>
    
    0 讨论(0)
提交回复
热议问题