php 如何通过php获取屏幕宽度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16996551/
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
how to get screen width through php?
提问by Pramod
Hi i know that php is server side thing... but i want know how to get user screen width through php. i search on the net, they says i have to use javascript and php together. but i don't how to make work. please tell me in simplest way to get the screen width of the user through php. if it can done through javascript only then tell me the way to pass the width variable of the javascript to the php variable in integer format(not string).
嗨,我知道 php 是服务器端的东西...但我想知道如何通过 php 获取用户屏幕宽度。我在网上搜索,他们说我必须同时使用 javascript 和 php。但我不知道如何工作。请以最简单的方式告诉我通过php获取用户的屏幕宽度。如果它只能通过javascript完成,那么告诉我将javascript的宽度变量以整数格式(不是字符串)传递给php变量的方法。
please show me codes to do it... i'm really messed up in all this... thanks :)
请给我看代码来做这件事……我真的把这一切搞砸了……谢谢:)
回答by Yogus
Not possible with complete php
you might need to use javascript
too
Anyway try this
不可能完成php
你可能也需要使用javascript
无论如何试试这个
<?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>';
}
?>
回答by nana.chorage
I am not sure, But It may help you
我不确定,但它可能会帮助你
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
var width = $(window).width();
//This is ajax code which will send width to your php page in the screenWidth variable
$.ajax({
url: "example.php", //this will be your php page
data : {screenwidth:width}
}).done(function() {
$(this).addClass("done");
});
</script>
//example.php code is here :
<?php
echo $width = $_REQUEST['screenwidth'];
?>
回答by Robert Seddon-Smith
I agree that it is a bad idea to rely on screen width from the client but here, for your edification:
我同意依赖客户端的屏幕宽度是一个坏主意,但在这里,为了您的启发:
javascript:
javascript:
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","set_screen_width.php?s="+screen.width,false);
xmlhttp.send();
If you execute this script on loading the page, it will check the screen width and send it to your php file which needs to be something like:
如果您在加载页面时执行此脚本,它将检查屏幕宽度并将其发送到您的 php 文件,该文件需要类似于:
<?PHP
session_start();
$_SESSION['screen']['width']=$_GET['s'];
?>
All your other PHP pages can then access the $_SESSION variable vis:
然后所有其他 PHP 页面都可以访问 $_SESSION 变量,如下所示:
<?PHP
session_start();
$screen_width=$_SESSION['screen']['width'];
etc...
等等...
It is better not to try to send HTML that will only look good in one screen width. You can, but it's a losing game. You just don't know what the client is going to do with it. Suppose, for instance, that they have low vision and like to use a larger font.
最好不要尝试发送仅在一个屏幕宽度下看起来不错的 HTML。你可以,但这是一场失败的游戏。你只是不知道客户会用它做什么。例如,假设他们视力不佳并且喜欢使用较大的字体。