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


72

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

Answers:


170

使用来自的用户代理$_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


5
但事实并非如此,我发现。这些标题只是来自手机中的Safari。如果您从某个应用程序连接,则它看起来像这样:app%20name / 1.0 CFNetwork / 485.13.8 Darwin / 11.0.0。因此,也许我可以寻找CFNetwork或Darwin或应用名称。
Snilleblixten 2011年

2
对于我不起作用,我正在为iPhone和iPad获得1.1 CFNetwork / 548.0.3 Darwin / 11.0.0:S现在我该如何区分它们?
阿萨德·汗

1
@AsadKhan穿了从模拟器进行测试吗?我也得到了同一个Http_user_agent
Kishor Kundan

1
更新为:$ iPadOS = stripos($ _ SERVER ['HTTP_USER_AGENT'],“ Macintosh”);
法迪

1
做工完美。已在2台iPhone上测试过,Ipad。感谢它的2020年!
Rodrigo Zuluaga

18
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;
}

7
function user_agent(){
    $iPod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");
    $iPhone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
    $iPad = strpos($_SERVER['HTTP_USER_AGENT'],"iPad");
    $android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
    file_put_contents('./public/upload/install_log/agent',$_SERVER['HTTP_USER_AGENT']);
    if($iPad||$iPhone||$iPod){
        return 'ios';
    }else if($android){
        return 'android';
    }else{
        return 'pc';
    }
}

4
$browser = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");

2
function isIosDevice(){
    $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
    $iosDevice = array('iphone', 'ipod', 'ipad');
    $isIos = false;

    foreach ($iosDevice as $val) {
        if(stripos($userAgent, $val) !== false){
            $isIos = true;
            break;
        }
    }

    return $isIos;
}

1
您应该添加!empty($_SERVER['HTTP_USER_AGENT']),因为$_SERVER['HTTP_USER_AGENT']当客户端不发送用户代理都没有设置。您的函数将崩溃,然后显示一条通知。
Thomas Kekeisen


0
<?php
$iPhone = false;
$AndroidPhone = false;
$deviceType = 0;

$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
print "<br>".$ua;

if(strpos($ua,"iphone") !== false ){
    $iPhone = true;
}
if(strpos($ua,"android") !== false){

    if(strpos($_SERVER['HTTP_USER_AGENT'],"mobile")){
        $AndroidPhone = true;        
    }
}
if(stripos($_SERVER['HTTP_USER_AGENT'],"iPad")){
    $iPad = true;
    $Tablet = true;
    $iOS = true;
}   

if($AndroidPhone==true || $iPhone==true)
{
    $deviceType = 1;
}
?>

0

51Degrees的PHP解决方案能够做到这一点。您可以在这里https://github.com/51Degrees/Device-Detection获得免费的开源API 。您可以使用HardwareFamily属性来确定它是否是iPad / iPod / iPhone等。

由于Apple User-Agent的性质,初始结果将返回通用设备,但是,如果您对特定设备感兴趣,则可以使用JavaScript客户端替代来确定特定型号。

为此,一旦确定它是Apple设备(在本例中为iPhone),则可以实现类似于以下逻辑的操作。

// iPhone model checks.
function getiPhoneModel() {
// iPhone 6 Plus
if ((window.screen.height / window.screen.width == 736 / 414) && 
(window.devicePixelRatio == 3)) {
return "iPhone 6 Plus";
}
// iPhone 6
else if ((window.screen.height / window.screen.width == 667 / 375) && 
(window.devicePixelRatio == 2)) {
return "iPhone 6";
}
// iPhone 5/5C/5S or 6 in zoom mode
else if ((window.screen.height / window.screen.width == 1.775) && 
(window.devicePixelRatio == 2)) {
return "iPhone 5, 5C, 5S or 6 (display zoom)";
}
// iPhone 4/4S
else if ((window.screen.height / window.screen.width == 1.5) && 
(window.devicePixelRatio == 2)) {
return "iPhone 4 or 4S";
}
// iPhone 1/3G/3GS
else if ((window.screen.height / window.screen.width == 1.5) && 
(window.devicePixelRatio == 1)) {
return "iPhone 1, 3G or 3GS";
} else {
return "Not an iPhone";
};
}

或用于iPad

function getiPadVersion() {
var pixelRatio = getPixelRatio();
var return_string = "Not an iPad";
if (pixelRatio == 1 ) {
return_string = "iPad 1, iPad 2, iPad Mini 1";
}
if (pixelRatio == 2) {
return_string = "iPad 3, iPad 4, iPad Air 1, iPad Air 2, iPad Mini 2, iPad 
Mini 3";
}
return return_string;
}

有关51学位对Apple设备所做的研究的更多信息,您可以在https://51degrees.com/blog/device-detection-for-apple-iphone-and-ipad上阅读其博客文章。

披露:我为51度工作。


0

为了响应Haim Evgi的代码,我在最后添加了!== false,以使其对我有用

$iPod    = stripos($_SERVER['HTTP_USER_AGENT'],"iPod") !== false;
$iPhone  = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone") !== false;
$iPad    = stripos($_SERVER['HTTP_USER_AGENT'],"iPad") !== false;
$Android = stripos($_SERVER['HTTP_USER_AGENT'],"Android") !== false;

最好将其添加为对他的答案的评论,即使我知道您发表评论已经有一段时间了。感谢您的代码!
Brian C

1
@BrianC叹为观止,Stack Overflow说我必须有50个声誉才能发表评论。
Gloson

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.