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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6322112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:00:46  来源:igfitidea点击:

Check if PHP-page is accessed from an iOS device

phpiphoneioshttp-headers

提问by Snilleblixten

I have a simple PHP webpage, and would like to return different content depending if it's accessed from an iPhone/iPad or from a web brower. How can I do that?

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

回答by Haim Evgi

Use the user agent from $_SERVER['HTTP_USER_AGENT'], and for simple detection you can use thisscript:

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

?> 

If you want to know more details of the user device I recommended to use one of the following solutions: http://51degrees.mobior http://deviceatlas.com

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

回答by Júlio Paulillo

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

回答by MrBii

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

回答by Pwnna

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

回答by Liuqing Hu

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

回答by Zarwalski

51Degrees' PHP solution is able to do this. you can get the free Open Source API here https://github.com/51Degrees/Device-Detection. You can use the HardwareFamily Property to determine if it is an iPad/iPod/iPhone etc.

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

Due to the nature of Apple's User-Agents the initial result will return a generic device, however if you are interested in the specific device you can use a JavaScript client side override to determine to specific model.

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

To do this you can implement something similar to the following logic once you have determined it is an Apple Device, in this case for an iPhone.

为此,您可以在确定它是 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";
};
}

Or for an iPad

或者 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;
}

For more information on research 51Degrees have done into Apple devices you can read their blog post here https://51degrees.com/blog/device-detection-for-apple-iphone-and-ipad.

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

Disclosure: I work for 51Degrees.

披露:我为 51Degrees 工作。

回答by Gloson

In response to Haim Evgi's code, I added !== false to the end for it to work for me

为了响应 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;

回答by Mohini

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

回答by Suresh Dhakal

It's work for Iphone

它适用于 Iphone

<?php
  $browser = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
  if ($browser == true){
    $browser = 'iphone';
  }
?>

回答by bohoej

If you just want to detect mobile devices in generel, Cake has build in support using RequestHandler->isMobile() (http://book.cakephp.org/2.0/en/core-libraries/components/request-handling.html#RequestHandlerComponent::isMobile)

如果你只是想检测一般的移动设备,Cake 已经使用 RequestHandler->isMobile() ( http://book.cakephp.org/2.0/en/core-libraries/components/request-handling.html# RequestHandlerComponent::isMobile)