使用 PHP 获取屏幕分辨率 [英] Getting the screen resolution using PHP

查看:52
本文介绍了使用 PHP 获取屏幕分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

使用纯 PHP 无法做到.你必须用 JavaScript 来完成.有几篇文章介绍了如何做到这一点.

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

本质上,您可以设置 cookie,甚至可以执行一些 Ajax 将信息发送到 PHP 脚本.如果你使用 jQuery,你可以这样做:

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)

<?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"));
}
?>

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

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...

这篇关于使用 PHP 获取屏幕分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆