首次加载时使用php获取用户的屏幕和视口尺寸 [英] Get user's screen&viewport dimensions in php on first load

查看:271
本文介绍了首次加载时使用php获取用户的屏幕和视口尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户访问页面时,我想用PHP获取屏幕和视口的高度/宽度.

I want to get in PHP the height/width of both the screen and the viewport when a user visits a page.

我尝试了不同的方法,但是它们都会引起其他问题.

I've tried different ways but they all cause other problems.

我的目标:

  • 获取有关首次加载的信息(没有跳转页面,没有重新加载).

  • Get the info on the first load (no jump page, no reload).

不更改网址

不影响在负载下运行的其余PHP,或使该PHP 运行两次

Not affect the rest of the PHP that runs on load, or make that PHP run twice

到目前为止我已经尝试过的:

What I've tried so far:

获取视口尺寸的Javascript:

Javascript to get viewport dimensions:

if(!isset($_POST['width']) || !isset($_POST['height'])) {
echo '
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
    var height = $(window).height();
    var width = $(window).width();
    $.ajax({
        type: \'POST\',
        data: {
            "height": height,
            "width": width
        },
        success: function (data) {
            $("body").html(data);
        },
    });
});
</script>
';
}

$user_width = $_POST['width'];
$user_height = $_POST['height'];

问题:导致所有php运行两次,一次加载,一次返回值(第二次在第一次返回后约4秒),这使得其余的php变得古怪……也使页面加载非常慢

problems: causes all php to run twice, once on load, once when it returns a value (the second time is about 4 seconds after the first), which makes the rest of the php wacky... also, makes page load very slow

将屏幕尺寸放入网址:

if(isset($_SESSION['screen_width']) AND isset($_SESSION['screen_height'])){
    $user_width = $_SESSION['screen_width'];
    $user_height = $_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>';
    }

问题:更改网址(这会影响其他代码,并且与现在的无表情网址相比看起来很糟糕);不返回视口大小afaik

problems: changes the url (which affects other code and looks bad compared to expressionless urls which they have now); doesn't return viewport size afaik

我是菜鸟.也许这很容易做到,但我真的不知道.我搜了很多.这就是我上面获得方法的方式.但是我无法让他们为我工作. Afaik,对于类似情况,我在任何地方都没有找到解决方案.

I'm a noob. Maybe it's easy to do, but I really don't know. I searched a lot. That's how I got methods above. But I couldn't get them to work for me. Afaik, I haven't seen a solution anywhere to a similar situation.

谢谢:)

推荐答案

如何将准备好文档的AJAX请求发送到单独的PHP文档,然后将其包含在页面加载头中.这样,您可以在准备好后将要加载的任何特定HTML直接放入正文中.

How about having your document ready AJAX request send to a separate PHP document, then include that in the head on page load. That way, you can have whatever specific HTML you want loaded put straight into the body when ready.

例如:

<head>
<script>
$(document).ready( function() {
    var height = $(window).height();
    var width = $(window).width();
    $.ajax({
        url: 'body.php',
        type: 'post',
        data: { 'width' : width, 'height' : height, 'recordSize' : 'true' },
        success: function(response) {
            $("body").html(response);
        }
    });
});
</script>
</head>
<body></body>

然后您的body.php文件将类似于:

And then your body.php file would look something like:

<?php
if(isset($_POST['recordSize'])) {
$height = $_POST['height'];
$width = $_POST['width'];
$_SESSION['screen_height'] = $height;
$_SESSION['screen_width'] = $width;
//Any html you want returned in here

如果您不希望该JS函数运行每个页面加载,只需将整个内容包装在<?php if(isset($_SESSION['screen_height'])) { //JS function } ?>

If you then didn't want that JS function to run every page load, just encase the whole thing in a <?php if(isset($_SESSION['screen_height'])) { //JS function } ?>

获取尺寸后,我不确定100%是否打算使用尺寸,因此,我的回答有些含糊,我对此表示歉意,但是通过这种方式,页面只会在尺寸设置中设置保存的尺寸第一次访问此浏览会话,它不必重定向,所以我希望它至少对您有所帮助:)

I'm not 100% sure what you were intending to do with the dimensions once you have them, so I apologise if my answer is a little vague, but by doing it this way the page only sets the saved dimensions on the first visit this browsing session, and it doesn't have to redirect, so I hope it's at least a little helpful :)

这篇关于首次加载时使用php获取用户的屏幕和视口尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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