本文介紹了PHP判斷手機是IOS還是Android的三個小實例,要判斷用戶的手機是安卓的還是ios的,搜了一下相關的資料,最終獲得的結果分享給大家。
實例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;}
最后“買3贈一”,再為大家分享一個與本主題關系不大的小實例:
php判斷頁面是否是微信打開
$user_agent = $_SERVER['HTTP_USER_AGENT']; if (strpos($user_agent, 'MicroMessenger') === false) { // 非微信瀏覽器禁止瀏覽 echo "HTTP/1.1 401 Unauthorized"; } else { // 微信瀏覽器,允許訪問 echo "MicroMessenger"; // 獲取版本號 preg_match('/.*?(MicroMessenger//([0-9.]+))/s*/', $user_agent, $matches); echo '<br>Version:'.$matches[2]; }
以上就是為大家分享的PHP判斷手機是IOS還是Android的三段代碼,希望大家喜歡,小編也會再接再厲,為大家提供更多實用的文章。