小编典典

使用PHP获取屏幕分辨率

php

我需要找到访问我的网站的用户屏幕的屏幕分辨率?


阅读 544

收藏
2020-05-26

共1个答案

小编典典

您无法使用纯PHP做到这一点。您必须使用JavaScript来完成。有几篇有关如何执行此操作的文章。

本质上,您可以设置cookie,甚至可以执行一些Ajax来将信息发送到PHP脚本。如果使用jQuery,则可以执行以下操作:

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)

<?php
// For instance, you can do something like this:
if(isset($_POST['width']) && isset($_POST['height'])) {
    $_SESSION['screen_width'] = $_POST['width'];
    $_SESSION['screen_height'] = $_POST['height'];
    echo json_encode(array('outcome'=>'success'));
} else {
    echo json_encode(array('outcome'=>'error','error'=>"Couldn't save dimension info"));
}
?>

所有这些实际上都是最基本的,但是它应该可以带您到某个地方。通常,屏幕分辨率不是您真正想要的。您可能对实际浏览器的视口的大小更感兴趣,因为这实际上是呈现页面的位置…

2020-05-26