使用PHP获得屏幕分辨率

使用PHP获得屏幕分辨率

我需要找到屏幕分辨率的用户谁访问我的网站屏幕?



慕容708150
浏览 975回答 3
3回答

月关宝盒

你不能用纯PHP来做这件事。您必须用JavaScript来完成它。有几篇关于如何做到这一点的文章。本质上,您可以设置cookie,甚至可以执行Ajax将信息发送到PHP脚本。如果使用jQuery,可以这样做:jQuery:$(function()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;$.post('some_script.php',&nbsp;{&nbsp;width:&nbsp;screen.width,&nbsp;height:screen.height&nbsp;},&nbsp;function(json)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(json.outcome&nbsp;==&nbsp;'success')&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;do&nbsp;something&nbsp;with&nbsp;the&nbsp;knowledge&nbsp;possibly? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert('Unable&nbsp;to&nbsp;let&nbsp;PHP&nbsp;know&nbsp;what&nbsp;the&nbsp;screen&nbsp;resolution&nbsp;is!'); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;},'json');});PHP(Somescript.php)<?php//&nbsp;For&nbsp;instance,&nbsp;you&nbsp;can&nbsp;do&nbsp;something&nbsp;like&nbsp;this:if(isset($_POST['width'])&nbsp;&&&nbsp;isset($_POST['height']))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;$_SESSION['screen_width']&nbsp;=&nbsp;$_POST['width']; &nbsp;&nbsp;&nbsp;&nbsp;$_SESSION['screen_height']&nbsp;=&nbsp;$_POST['height']; &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;json_encode(array('outcome'=>'success'));}&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;json_encode(array('outcome'=>'error','error'=>"Couldn't&nbsp;save&nbsp;dimension&nbsp;info"));}?>所有这些都是基本的,但它应该会让你有所收获。通常情况下,屏幕分辨率并不是你真正想要的。您可能更感兴趣的是实际浏览器的视图端口的大小,因为这实际上是页面呈现的地方.

精慕HU

直接使用PHP是不可能的,但是.。我编写了这个简单的代码来保存PHP会话上的屏幕分辨率,以便在图片库中使用。<?php session_start();if(isset($_SESSION['screen_width'])&nbsp;AND&nbsp;isset($_SESSION['screen_height'])){ &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;'User&nbsp;resolution:&nbsp;'&nbsp;.&nbsp;$_SESSION['screen_width']&nbsp;.&nbsp;'x'&nbsp;.&nbsp;$_SESSION['screen_height'];}&nbsp;else&nbsp;if(isset($_REQUEST['width'])&nbsp;AND&nbsp;isset($_REQUEST['height']))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;$_SESSION['screen_width']&nbsp;=&nbsp;$_REQUEST['width']; &nbsp;&nbsp;&nbsp;&nbsp;$_SESSION['screen_height']&nbsp;=&nbsp;$_REQUEST['height']; &nbsp;&nbsp;&nbsp;&nbsp;header('Location:&nbsp;'&nbsp;.&nbsp;$_SERVER['PHP_SELF']);}&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;'<script&nbsp;type="text/javascript">window.location&nbsp;=&nbsp;"'&nbsp;.&nbsp;$_SERVER['PHP_SELF']&nbsp;.&nbsp;'?width="+screen.width+"&height="+screen.height;</script>';}?>
打开App,查看更多内容
随时随地看视频慕课网APP