使用PHP获取屏幕分辨率
-
19-09-2019 - |
题
我需要找到访问我网站的用户屏幕的屏幕分辨率?
解决方案
您无法用纯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(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"));
}
?>
所有这一切真的是基本的,但它应该让你的地方。通常,屏幕分辨率是不是你真正想要的,虽然。你可能会更感兴趣的是实际的浏览器视口的大小,因为,实际上是在呈现页面...
其他提示
直接与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>';
}
?>
PHP是一个服务器端语言 - 它仅在服务器上执行,所得的程序输出被发送到客户端。这样,没有“客户端屏幕”信息用。
这是说,你可以让客户告诉你他们的屏幕分辨率是通过JavaScript什么。写一个小的scriptlet向您发送screen.width和screen.height - 可能通过AJAX,或更可能与最初的“跳页”那找到它,然后重定向到的 http://example.net/index.php?size=AxB
虽然说作为一个用户,我更喜欢你设计一个网站,流体处理任何屏幕分辨率。我浏览在不同尺寸的窗口,大多没有最大化。
这是一个非常简单的过程。是的,你不能在PHP的宽度和高度。这是真正的jQuery可提供屏幕的宽度和高度。首先去 https://github.com/carhartl/jquery-cookie 并得到jquery.cookie .js文件。下面是示例使用PHP,以获得屏幕宽度和高度:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script src="js/jquery.cookie.js"></script>
<script type=text/javascript>
function setScreenHWCookie() {
$.cookie('sw',screen.width);
$.cookie('sh',screen.height);
return true;
}
setScreenHWCookie();
</script>
</head>
<body>
<h1>Using jquery.cookie.js to store screen height and width</h1>
<?php
if(isset($_COOKIE['sw'])) { echo "Screen width: ".$_COOKIE['sw']."<br/>";}
if(isset($_COOKIE['sh'])) { echo "Screen height: ".$_COOKIE['sh']."<br/>";}
?>
</body>
</html>
我有一个测试,你可以执行: http://rw-wrd.net/test.php
使用JavaScript(screen.width
和screen.height
IIRC,但我可能是错的,也没有在一段时间做JS)。 PHP不能做到这一点。
我发现用我的HTML中的CSS里面我的PHP做的把戏我。
<?php
echo '<h2 media="screen and (max-width: 480px)">';
echo 'My headline';
echo '</h2>';
echo '<h1 media="screen and (min-width: 481px)">';
echo 'My headline';
echo '</h1>';
?>
这会输出一个较小尺寸的标题,如果屏幕是480像素或更小。 因此,没有必要通过使用JS或类似任何VARS。
可以尝试RESS(自适应设计+服务器端组件),请参见本教程:
我不认为你可以检测屏幕尺寸纯粹用PHP,但你可以检测用户代理..
<?php
if ( stristr($ua, "Mobile" )) {
$DEVICE_TYPE="MOBILE";
}
if (isset($DEVICE_TYPE) and $DEVICE_TYPE=="MOBILE") {
echo '<link rel="stylesheet" href="/css/mobile.css" />'
}
?>
下面是一个更详细的脚本的链接: PHP移动检测
完全工作示例
我找不到实际可用的 PHP 示例 “无形地” (不带 URL 参数) 返回客户端屏幕尺寸, ,以及其他属性,到服务器端 PHP,所以我把这个例子放在一起。
JS 填充并提交隐藏表单(由 PHP 从 JS 属性数组编写脚本), POST
本身(现在在 PHP 中可用的数据)并返回表中的数据。
(在“几个”浏览器中测试。)
<!DOCTYPE html>
<html>
<head>
<title>*Client Info*</title>
<style>table,tr{border:2px solid gold;border-collapse:collapse;}td{padding:5px;}</style>
</head>
<body>
<?php
$clientProps=array('screen.width','screen.height','window.innerWidth','window.innerHeight',
'window.outerWidth','window.outerHeight','screen.colorDepth','screen.pixelDepth');
if(! isset($_POST['screenheight'])){
echo "Loading...<form method='POST' id='data' style='display:none'>";
foreach($clientProps as $p) { //create hidden form
echo "<input type='text' id='".str_replace('.','',$p)."' name='".str_replace('.','',$p)."'>";
}
echo "<input type='submit'></form>";
echo "<script>";
foreach($clientProps as $p) { //populate hidden form with screen/window info
echo "document.getElementById('" . str_replace('.','',$p) . "').value = $p;";
}
echo "document.forms.namedItem('data').submit();"; //submit form
echo "</script>";
}else{
echo "<table>";
foreach($clientProps as $p) { //create output table
echo "<tr><td>".ucwords(str_replace('.',' ',$p)).":</td><td>".$_POST[str_replace('.','',$p)]."</td></tr>";
}
echo "</table>";
}
?>
<script>
window.history.replaceState(null,null); //avoid form warning if user clicks refresh
</script>
</body>
</html>
返回的数据是 extract
变成变量。例如:
window.innerWidth
返回于$windowinnerWidth
的唯一方法是使用JavaScript,然后得到的JavaScript张贴到它到PHP(如果你真的需要有水库服务器端)。然而,这将完全地落在平坦在其表面上,如果他们把JavaScript的关闭。
JS:
$.ajax({
url: "ajax.php",
type: "POST",
data: "width=" + $("body").width(),
success: function(msg) {
return true;
}
});
ajax.php
if(!empty($_POST['width']))
$width = (int)$_POST['width'];
使用 cookie 可以轻松完成此操作。此方法允许页面根据屏幕高度和宽度(或浏览器视图端口高度和宽度值)检查存储的 cookie 值,如果它们不同,则会重置 cookie 并重新加载页面。该代码需要考虑到用户的偏好。如果持久 cookie 已关闭,请使用会话 cookie。如果这不起作用,您必须使用默认设置。
- JavaScript:检查高度和宽度 cookie 是否设置
- JavaScript:如果设置,请检查 screen.height 和 screen.width (或任何你想要的)是否与 cookie 的当前值匹配
- JavaScript:如果 cookie 未设置或者与当前值不匹配,则:A。JavaScript:创建名为(例如)“shw”的持久或会话 cookie,并将其设置为当前 screen.height 和 screen.width 的值。
b.JavaScript:使用 window.location.reload() 重定向到 SELF。重新加载时,将跳过步骤 3。 - PHP:$_COOKIE['shw'] 包含值。
- 继续使用 PHP
例如,我正在使用一些在网络上找到的常见 cookie 功能。确保 setCookie 返回正确的值。我将此代码放在 head 标签之后。显然该函数应该位于源文件中。
<head>
<script src="/include/cookielib.js"></script>
<script type=text/javascript>
function setScreenHWCookie() {
// Function to set persistant (default) or session cookie with screen ht & width
// Returns true if cookie matches screen ht & width or if valid cookie created
// Returns false if cannot create a cookies.
var ok = getCookie( "shw");
var shw_value = screen.height+"px:"+screen.width+"px";
if ( ! ok || ok != shw_value ) {
var expires = 7 // days
var ok = setCookie( "shw", shw_value, expires)
if ( ok == "" ) {
// not possible to set persistent cookie
expires = 0
ok = setCookie( "shw", shw_value, expires)
if ( ok == "" ) return false // not possible to set session cookie
}
window.location.reload();
}
return true;
}
setScreenHWCookie();
</script>
....
<?php
if( isset($_COOKIE["shw"])) {
$hw_values = $_COOKIE["shw"];
}
您可以设置窗口的宽度在前端使用JS饼干,你可以在PHP中得到它:
<script type="text/javascript">
document.cookie = 'window_width='+window.innerWidth+'; expires=Fri, 3 Aug 2901 20:47:11 UTC; path=/';
</script>
<?PHP
$_COOKIE['window_width'];
?>
简单的回答是否定的,那么你可能会问,为什么我不能做到这一点用PHP。 OK这里是一个较长的答案。 PHP是一种服务器端脚本语言和为此无关与特定的客户端类型。然后,你可能会问:“为什么我可以再从PHP获得浏览器代理?”,那是因为该信息,通过向服务器发出请求与最初的HTTP头一起发送。所以,如果你想,这不是与HTTP标头发送客户信息则必须在客户端脚本语言,像JavaScript。
在 PHP 中,没有标准方法来获取此信息。但是,如果您使用第三方解决方案,则有可能。适用于 PHP 的 51Degrees 设备检测器具有您需要的属性:
为您提供用户屏幕的宽度和高度(以像素为单位)。为了使用这些属性,您需要从以下位置下载检测器 来源锻造. 。然后,您需要在需要检测屏幕高度和宽度的文件中包含以下两行:
<?php
require_once 'path/to/core/51Degrees.php';
require_once 'path/to/core/51Degrees_usage.php';
?>
其中 path/to/core 是您从 sourceforge 下载的“Core”目录的路径。最后,使用属性:
<?php
echo $_51d['ScreenPixelsHeight']; //Output screen height.
echo $_51d['ScreenPixelsWidth']; //Output screen width.
?>
请记住,当无法识别设备时,这些变量有时可能包含“未知”值。
解决方案:使可扩展的网络设计......(我们应该怎么说呢适当的网页设计)格式化应该做的客户端,我也希望信息,将向下传递到服务器,但信息仍然是有用的(多少每个对象排种交易),但仍网页设计应该是流体从而每一行元件不应该投入,除非其表的实际表(和数据将扩展到它的单个细胞)如果你使用一个div可以叠加的每个元素旁边对方和你的窗口应该“打破”行在适当的位置。 (只需要适当的CSS)
PHP只对服务器端,而不是用户的主机上。使用JavaScript或jQuery来得到这个信息,并通过AJAX或URL(X = 1024&Y = 640)发送。
最简单的方法
<?php
//-- you can modified it like you want
echo $width = "<script>document.write(screen.width);</script>";
echo $height = "<script>document.write(screen.height);</script>";
?>
<script type="text/javascript">
if(screen.width <= 699){
<?php $screen = 'mobile';?>
}else{
<?php $screen = 'default';?>
}
</script>
<?php echo $screen; ?>