2014-11-14 116 views
0

我不确定如果我问这个正确的方法。让我解释一下...... 我注意到facebook是“不是”响应式网页,因为当我更改我的笔记本电脑上的浏览器宽度时,facebook.com上的浏览器不会改变。另一方面,当我使用我的手机并在浏览器facebook.com中运行时,我已经知道我使用的是移动设备,并且它的大小适合我的设备。如何判断访问者是否通过手机访问网站?

问题: 它是如何工作的,这是一种伎俩来优化我们的网站?

+0

这个以前的问题/答案可能会回答你的问题 - http://stackoverflow.com/questions/15751011/is-there -a路到执行-A-移动侦测,也就是说,正是百搭Facebook的移动 – 2014-11-14 23:43:33

回答

1

我不完全知道Facebook,但如果没有JavaScript或CSS媒体查询检测的迹象,那么他们可能是在旧式的方式,即user agent strings

每当您对网站的要求,您的浏览器请求报头中发送一个字符串,在我的情况是:

User-Agent: Mozilla/5.0 (X11; Linux i686; rv:32.0) Gecko/20100101 Firefox/32.0 Iceweasel/32.0a2 

接收到请求可以使用该字符串来获得的想法服务器你使用的是什么操作系统和浏览器,你可以看到,在我的情况下,它是操作系统的Debian和浏览器的Iceweasel(= firefox),你可以确定我正在浏览桌面计算机。

0

我相信用户代理将是您的最佳选择。

navigator.userAgent 
0
当我们浏览我们的台式机服务器检测,我们是在桌面上,它只需发送默认

(桌面)fb的索引页,如果我们使用的是其他客户端,这是不是在另一方面responsive.but作为平板电脑或手机,服务器检测到它,因此它将网络文档重定向到fb的其他移动友好页面,或者它会改变它的CSS,它不像桌面page.It意味着服务器检测我们的设备和浏览器。这种可用于检测的php检测脚本是:

//php technique 
<?php 
$agent = $_SERVER['HTTP_USER_AGENT']; // Put browser name into local variable 
if (preg_match("/iPhone/", $agent)) { 
header("location: iphone_home.html"); 
} else if (preg_match("/android/", $agent)) { 
header("location: android_home.html"); 
}?> 

//javascript technique 
<script language="javascript" type="text/javascript"> 

var agent = navigator.userAgent.toLowerCase(); 
if (agent.indexOf('iphone') != -1) { // iPhone Device 

    // If it is an iPhone put specific code to run here for iPhone users 

} else if (agent.indexOf('android') != -1) { // Google phones running Android OS 

    // If it is a Google phone put specific code to run here for Android users 

} 
</script> 

//php technique to detect OS and browser of user 

<?php 
$agent = $_SERVER['HTTP_USER_AGENT']; 
$browserArray = array(
    'Windows Mobile' => 'IEMobile', 
'Android Mobile' => 'Android', 
'iPhone Mobile' => 'iPhone', 
'Firefox' => 'Firefox', 
    'Google Chrome' => 'Chrome', 
    'Internet Explorer' => 'MSIE', 
    'Opera' => 'Opera', 
    'Safari' => 'Safari' 
); 
foreach ($browserArray as $k => $v) { 

if (preg_match("/$v/", $agent)) { 
    break; 
} else { 
$k = "Browser Unknown"; 
} 
} 
$browser = $k; 
$osArray = array(
    'Windows 98' => '(Win98)|(Windows 98)', 
    'Windows 2000' => '(Windows 2000)|(Windows NT 5.0)', 
'Windows ME' => 'Windows ME', 
    'Windows XP' => '(Windows XP)|(Windows NT 5.1)', 
    'Windows Vista' => 'Windows NT 6.0', 
    'Windows 7' => '(Windows NT 6.1)|(Windows NT 7.0)', 
    'Windows NT 4.0' => '(WinNT)|(Windows NT 4.0)|(WinNT4.0)|(Windows NT)', 
'Linux' => '(X11)|(Linux)', 
'Mac OS' => '(Mac_PowerPC)|(Macintosh)|(Mac OS)' 
); 
foreach ($osArray as $k => $v) { 

if (preg_match("/$v/", $agent)) { 
    break; 
} else { 
$k = "Unknown OS"; 
} 
} 
$os = $k; 

echo $agent; 
echo "<h2>You are using: <em>$browser - $os</em></h2>"; 
?>