Getting the screen resolution using PHP

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

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

21条回答
  •  猫巷女王i
    2020-11-22 07:12

    You can't do it with pure PHP. You must do it with JavaScript. There are several articles written on how to do this.

    Essentially, you can set a cookie or you can even do some Ajax to send the info to a PHP script. If you use jQuery, you can do it something like this:

    jquery:

    $(function() {
        $.post('some_script.php', { width: screen.width, height:screen.height }, function(json) {
            if(json.outcome == 'success') {
                // do something with the knowledge possibly?
            } else {
                alert('Unable to let PHP know what the screen resolution is!');
            }
        },'json');
    });
    

    PHP (some_script.php)

    'success'));
    } else {
        echo json_encode(array('outcome'=>'error','error'=>"Couldn't save dimension info"));
    }
    ?>
    

    All that is really basic but it should get you somewhere. Normally screen resolution is not what you really want though. You may be more interested in the size of the actual browser's view port since that is actually where the page is rendered...

提交回复
热议问题