检查 PHP 页面是否从 iOS 设备访问

2022-08-30 08:01:52

我有一个简单的PHP网页,并希望返回不同的内容,具体取决于它是从iPhone / iPad还是从网络浏览器访问的。我该怎么做?


答案 1

使用 中的用户代理,对于简单的检测,您可以使用以下脚本:$_SERVER['HTTP_USER_AGENT']

<?php

//Detect special conditions devices
$iPod    = stripos($_SERVER['HTTP_USER_AGENT'],"iPod");
$iPhone  = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$iPad    = stripos($_SERVER['HTTP_USER_AGENT'],"iPad");
$Android = stripos($_SERVER['HTTP_USER_AGENT'],"Android");
$webOS   = stripos($_SERVER['HTTP_USER_AGENT'],"webOS");

//do something with this information
if( $iPod || $iPhone ){
    //browser reported as an iPhone/iPod touch -- do something here
}else if($iPad){
    //browser reported as an iPad -- do something here
}else if($Android){
    //browser reported as an Android device -- do something here
}else if($webOS){
    //browser reported as a webOS device -- do something here
}

?> 

如果您想了解用户设备的更多详细信息,我建议使用以下解决方案之一:http://51degrees.mobihttp://deviceatlas.com


答案 2
preg_match("/iPhone|Android|iPad|iPod|webOS/", $_SERVER['HTTP_USER_AGENT'], $matches);
$os = current($matches);

switch($os){
   case 'iPhone': /*do something...*/ break;
   case 'Android': /*do something...*/ break;
   case 'iPad': /*do something...*/ break;
   case 'iPod': /*do something...*/ break;
   case 'webOS': /*do something...*/ break;
}

推荐