2014-11-14 121 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>"; 
?>