我有一个简单的PHP网页,并希望返回不同的内容,具体取决于它是从iPhone / iPad还是从网络浏览器访问的。我怎么能这样做?
答案 0 :(得分:147)
使用$_SERVER['HTTP_USER_AGENT']
中的用户代理,
对于简单的检测,您可以使用this脚本:
<?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.mobi或http://deviceatlas.com
答案 1 :(得分:14)
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;
}
答案 2 :(得分:4)
$browser = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
答案 3 :(得分:4)
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 :(得分:1)
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;
}
答案 5 :(得分:0)
这适用于Iphone
<?php
$browser = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
if ($browser == true){
$browser = 'iphone';
}
?>
答案 6 :(得分:0)
如果您只是想检测一般的移动设备,Cake已使用请求处理程序支持内置支持 - &gt; isMobile()(http://book.cakephp.org/2.0/en/core-libraries/components/request-handling.html#RequestHandlerComponent::isMobile)
答案 7 :(得分: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;
}
?>
答案 8 :(得分:0)
51Degrees&#39; PHP解决方案能够做到这一点。您可以在此处获取免费的开源API https://github.com/51Degrees/Device-Detection。您可以使用HardwareFamily属性来确定它是否是iPad / iPod / iPhone等。
由于Apple用户代理的性质,初始结果将返回通用设备,但是如果您对特定设备感兴趣,则可以使用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;
}
有关51Degrees已经对Apple设备进行的研究的更多信息,您可以在此处阅读他们的博客文章https://51degrees.com/blog/device-detection-for-apple-iphone-and-ipad。
披露:我为51Degrees工作。
答案 9 :(得分: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;