为了账号安全,请及时绑定邮箱和手机立即绑定

使用PHP获得屏幕分辨率

使用PHP获得屏幕分辨率

PHP
慕容708150 2019-06-29 10:13:06
使用PHP获得屏幕分辨率我需要找到屏幕分辨率的用户谁访问我的网站屏幕?
查看完整描述

3 回答

?
月关宝盒

TA贡献1772条经验 获得超5个赞

你不能用纯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(Somescript.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"));}?>

所有这些都是基本的,但它应该会让你有所收获。通常情况下,屏幕分辨率并不是你真正想要的。您可能更感兴趣的是实际浏览器的视图端口的大小,因为这实际上是页面呈现的地方.


查看完整回答
反对 回复 2019-06-29
?
精慕HU

TA贡献1845条经验 获得超8个赞

直接使用PHP是不可能的,但是.。

我编写了这个简单的代码来保存PHP会话上的屏幕分辨率,以便在图片库中使用。

<?php
session_start();if(isset($_SESSION['screen_width']) AND isset($_SESSION['screen_height'])){
    echo 'User resolution: ' . $_SESSION['screen_width'] . 'x' . $_SESSION['screen_height'];} else if(isset($_REQUEST['width']) AND isset($_REQUEST['height'])) {
    $_SESSION['screen_width'] = $_REQUEST['width'];
    $_SESSION['screen_height'] = $_REQUEST['height'];
    header('Location: ' . $_SERVER['PHP_SELF']);} else {
    echo '<script type="text/javascript">window.location = "' . $_SERVER['PHP_SELF'] . '?width="+screen.width+"&height="+screen.height;</script>';}?>


查看完整回答
反对 回复 2019-06-29
  • 3 回答
  • 0 关注
  • 974 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信