首次加载时在 php 中获取用户的屏幕和视口尺寸

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18101207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:58:54  来源:igfitidea点击:

Get user's screen&viewport dimensions in php on first load

phpresolutiondimensions

提问by kenji359

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

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

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

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

My goals:

我的目标:

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

  • Not change the url

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

  • 获取第一次加载的信息(没有跳转页面,没有重新加载)。

  • 不改变网址

  • 不影响加载时运行的 PHP 的其余部分,或使该 PHP 运行两次

What I've tried so far:

到目前为止我尝试过的:

Javascript to get viewport dimensions:

获取视口尺寸的Javascript:

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'];

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

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

Putting the screen dimensions into the url:

将屏幕尺寸放入网址:

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>';
    }

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

问题:更改 url(这会影响其他代码并且与他们现在拥有的无表情 url 相比看起来很糟糕);不返回视口大小 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.

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

Thanks :)

谢谢 :)

回答by Matt

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.

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

For example:

例如:

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

And then your body.phpfile would look something like:

然后你的body.php文件看起来像:

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

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 } ?>

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

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 :)

我不是 100% 确定您在拥有尺寸后打算对它们做什么,所以如果我的回答有点含糊,我深表歉意,但通过这样做,页面只会在第一次访问时设置保存的尺寸浏览会话,它不必重定向,所以我希望它至少有一点帮助:)

回答by Matthijs

I think it's better to ask yourself: What do you want to do with the widthand height? And look if there are other ways to accomplish that goal. You cannot get the user his screen width and height using PHP because it runs server-side.

我认为最好问问自己:你想用widthand做height什么?看看是否还有其他方法可以实现这一目标。您无法使用 PHP 获取用户的屏幕宽度和高度,因为它在服务器端运行。

So this data is neveravailable on the first load.

所以这个数据在第一次加载时永远不可用。

You could submit the data using AJAX and then make the changes to the page using JavaScript. Then the next page reload the data is, when stored, server-sided available.

您可以使用 AJAX 提交数据,然后使用 JavaScript 对页面进行更改。然后下一页重新加载数据,当存储时,服务器端可用。