Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 客户端显示分辨率检测_Php_Javascript_Ajax_Resolution - Fatal编程技术网

Php 客户端显示分辨率检测

Php 客户端显示分辨率检测,php,javascript,ajax,resolution,Php,Javascript,Ajax,Resolution,目前,我正在使用以下PHP+JavaScript代码来检测客户端浏览器窗口大小 $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; if(isSet($_GET['w']) && isSet($_GET['h'])) { $w = $_GET['w']; $h = $_GET['h']; } if(!$w && !$h) { $pos= strpos($url, '?');

目前,我正在使用以下PHP+JavaScript代码来检测客户端浏览器窗口大小

$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if(isSet($_GET['w']) && isSet($_GET['h']))
{
$w = $_GET['w']; 
$h = $_GET['h'];
}


if(!$w && !$h)
{
    $pos= strpos($url, '?');
    if($pos===false)
    {
echo '<script type="text/javascript">';
echo "var w = screen.width;
var h = screen.height;
window.location.href = '".$url."?w='+w+'&h='+h;
</script>'";
}

else {
echo '<script type="text/javascript">';
echo "var w = screen.width;
var h = screen.height;
window.location.href =  '".$url."&w='+w+'&h='+h;
</script>'";
}
}
$url=“http://”$\u服务器['http\u主机]。$\u服务器['REQUEST\u URI'];
if(isSet($\u GET['w'])和&isSet($\u GET['h']))
{
$w=$_GET['w'];
$h=$_GET['h'];
}
如果(!$w&&!$h)
{
$pos=strpos($url,“?”);
如果($pos==false)
{
回声';
echo“var w=屏幕宽度;
var h=屏幕高度;
window.location.href='“$url.”?w='+w+'&h='+h;
'";
}
否则{
回声';
echo“var w=屏幕宽度;
var h=屏幕高度;
window.location.href='“$url.”&w='+w+'&h='+h;
'";
}
}

我想知道,有没有什么方法可以优化代码(通过ajax发布等等)或者简单的方法呢

不太可能,因为您必须先往返于broser以确定屏幕大小

如果您只需要后续请求的屏幕大小,可以执行ajax调用并将值保存在会话中(jquery示例):

这里没有ajax

if ( $_GET["setscreen"] ) $_SESSION['display_resolution'] = $_GET['screenwidth'] . 'x' . $_GET['screenheight'];
if ( $_GET["setscreen"] ) header('Location: ' . $_SERVER['PHP_SELF']);
if ( ! $_SESSION['display_resolution'] ) echo '<script type="text/javascript">window.location = "' . $_SERVER['PHP_SELF'] . '?setscreen=1&screenwidth="+screen.width+"&screenheight="+screen.height;</script>';
if($\u GET[“设置屏幕])$\u会话['display\u resolution']=$\u GET['screenwidth'])x’$_获取[‘屏幕高度’];
if($_GET[“setscreen”])头('Location:'。$_服务器['PHP_SELF']);
如果(!$_会话['display_resolution'])echo'window.location=“”。$_服务器['PHP_SELF']”?设置屏幕=1&screenwidth=“+screen.width+”&screenheight=“+screen.height;”;

会话显示分辨率包含一个类似于:1280 x 1024的字符串。我不熟悉js,有没有办法回显
var w
var h
?我的意思是类似的
(比如php)。在我的例子中,我想把它们发送到
css.php
。在php中,看起来是这样的:
Yes,您可以执行如下操作:
var headID=document.getElementsByTagName(“head”)[0];var cssNode=document.createElement('link');cssNode.type='text/css';cssNode.rel='样式表';cssNode.href='core/design/css.php?w='+w+'&h='+h;cssNode.media='screen';头部ID.儿童(cssNode)但要注意:禁用Javascript的访问者将完全没有样式。你这样做的目的是什么?这可能根本没有必要。听起来你是在用它来指定要发送的CSS数据(基于下面的评论),但是一个CSS文件可以处理多个截然不同的分辨率,所以这真的不是必需的(而且对于禁用JS的客户机是有效的,并且消除了往返)。若它是用于分析的,那个么您可以在每个页面加载时使用w/h发送一个AJAX请求(或者无论您希望的频率如何,比如只发送新会话)。
if ( $_GET["setscreen"] ) $_SESSION['display_resolution'] = $_GET['screenwidth'] . 'x' . $_GET['screenheight'];
if ( $_GET["setscreen"] ) header('Location: ' . $_SERVER['PHP_SELF']);
if ( ! $_SESSION['display_resolution'] ) echo '<script type="text/javascript">window.location = "' . $_SERVER['PHP_SELF'] . '?setscreen=1&screenwidth="+screen.width+"&screenheight="+screen.height;</script>';