本篇文章主要介紹PHP判斷手機是IOS還是Android的三個方法,感興趣的朋友參考下,希望對大家有所協助。
執行個體1:主要是要用到HTTP_USER_AGENT,它表示的意思是用來檢查瀏覽頁面的訪問者在用什麼作業系統(包括版本號碼)瀏覽器(包括版本號碼)和使用者個人偏好的代碼。
監測代碼如下:
function get_device_type(){ //全部變成小寫字母 $agent = strtolower($_SERVER['HTTP_USER_AGENT']); $type = 'other'; //分別進行判斷 if(strpos($agent, 'iphone') || strpos($agent, 'ipad')){ $type = 'ios'; } if(strpos($agent, 'android')){ $type = 'android'; } return $type;}
通過調用Objective-C這個函數,就能擷取到手機的類型。
執行個體2:只需要一個判斷就好
<?phpif(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){ echo 'systerm is IOS';}else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){ echo 'systerm is Android';}else{ echo 'systerm is other';}?>
執行個體3:這個執行個體可能有些偏題不過也分享給大家
function get_device_type(){ //全部變成小寫字母 $agent = strtolower($_SERVER['HTTP_USER_AGENT']); $type ='other'; //分別進行判斷 if(strpos($agent,'iphone') || strpos($agent,'ipad')){ $type ='ios'; } if(strpos($agent,'android')){ $type ='android'; } return$type;}
總結:以上就是本篇文的全部內容,希望能對大家的學習有所協助。