2011-03-17 24 views

回答

7

MobileESP是全面的,但不錯&易於使用。

+0

非常感謝所有三個真的幫助完整。 – 2011-03-17 06:00:34

11

檢查移動用戶代理$_SERVER['HTTP_USER_AGENT']

您應該檢出http://detectmobilebrowser.com/已有的腳本以檢測移動瀏覽器(它只是使用用戶代理)。

+4

+1對於有用的鏈接 – diEcho 2011-03-17 05:40:05

15

我正在使用一項功能來識別我的項目中的移動瀏覽器,它可以檢測幾乎所有主要的移動操作系統和瀏覽器。

function ismobile() { 
    $is_mobile = '0'; 

    if(preg_match('/(android|up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) { 
     $is_mobile=1; 
    } 

    if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) { 
     $is_mobile=1; 
    } 

    $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4)); 
    $mobile_agents = array('w3c ','acs-','alav','alca','amoi','andr','audi','avan','benq','bird','blac','blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno','ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-','maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-','newt','noki','oper','palm','pana','pant','phil','play','port','prox','qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar','sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-','tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp','wapr','webc','winw','winw','xda','xda-'); 

    if(in_array($mobile_ua,$mobile_agents)) { 
     $is_mobile=1; 
    } 

    if (isset($_SERVER['ALL_HTTP'])) { 
     if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini')>0) { 
      $is_mobile=1; 
     } 
    } 

    if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) { 
     $is_mobile=0; 
    } 

    return $is_mobile; 
} 
1

http://mobiledetect.net/是另一種輕量級的PHP類,但你薄霧記住,檢查用戶代理是一個很好的,但不完美的方式,這個問題是規則不斷過時和不完整,因此您需要不斷更改檢測代碼。 另外檢查https://modernizr.com/另一種檢測方式。

0

現在,你認爲什麼是「移動設備」?

用戶代理解析器會給出非常好的結果,除了罕見的情況。問題在於,如果您在本地存儲數據,或者如果您在「雲中」使用它,則依賴於正在聯機的服務,您必須不斷更新它們。

最好使用功能檢測庫,例如Modernizr,首次訪問時向您的服務器發送有關瀏覽器功能的信息,並根據瀏覽器可以或不可以執行的操作提供適當的內容。或者甚至更好,委託給Javascript。

0

這是在PHP中完成它的最簡單的方法。

if(stristr($_SERVER['HTTP_USER_AGENT'],'Mobile')){ 
    //Put code here when device is mobile 
}else{ 
    //Put code here when device is not a mobile 
} 
相關問題