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

查看:218
本文介绍了使用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天全站免登陆