在實務上一定會有些人想知道瀏覽者用什麼裝置上來網站
來做一些介面上的處理 這裡分享一隻function
// 取得瀏覽者裝置
function userAgent($ua){ $iphone = strstr(strtolower($ua), 'mobile'); //Search for 'mobile' in user-agent (iPhone have that) $android = strstr(strtolower($ua), 'android'); //Search for 'android' in user-agent $windowsPhone = strstr(strtolower($ua), 'phone'); //Search for 'phone' in user-agent (Windows Phone uses that) function androidTablet($ua){ //Find out if it is a tablet if(strstr(strtolower($ua), 'android') ){//Search for android in user-agent if(!strstr(strtolower($ua), 'mobile')){ //If there is no ''mobile' in user-agent (Android have that on their phones, but not tablets) return true; } } } $androidTablet = androidTablet($ua); //Do androidTablet function $ipad = strstr(strtolower($ua), 'ipad'); //Search for iPad in user-agent if($androidTablet || $ipad){ //If it's a tablet (iPad / Android) return 'tablet'; } elseif($iphone && !$ipad || $android && !$androidTablet || $windowsPhone){ //If it's a phone and NOT a tablet return 'mobile'; } else{ //If it's not a mobile device return 'desktop'; } }
使用時只要傳進php全域變數$_SERVER['HTTP_USER_AGENT']
就可以輕鬆取得瀏覽者使用的裝置
userAgent($_SERVER['HTTP_USER_AGENT'])
如果傳回值為
1.desktop 代表PC
2.mobile 代表手機
3.tablet 代表平板
維度自動免費架站系統,讓你快速擁有自己的網站
維度架站(新版系統)
維度架站
維度雲商城
全站熱搜
留言列表