Check if PHP page is accessible from iOS device

I have a simple PHP web page and I would like to return other content depending on access to it from iPhone / iPad or a web browser. How can i do this?

+56
php ios iphone
Jun 12 '11 at 13:09
source share
10 answers

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

 <?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 detailed information about the user device, I recommended using one of the following solutions: http://51degrees.mobi or http://deviceatlas.com

+141
Jun 12 '11 at 13:13
source share
 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; } 
+14
Mar 12 '14 at 22:03
source share
 $browser = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone"); 
+4
Jun 12 '11 at 13:15
source share
 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
Jun 25 '14 at 7:24
source share

It works for iphone

 <?php $browser = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone"); if ($browser == true){ $browser = 'iphone'; } ?> 
0
Apr 15 '13 at 11:13
source share

If you just want to discover mobile devices in general, Cake has built-in support using RequestHandler-> isMobile () ( http: //book.cakephp. Org / 2.0 / a / core-library / components / request-handling.html # RequestHandlerComponent :: isMobile )

0
May 7 '14 at 7:57
source share
 <?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
Oct 29 '15 at 11:35
source share
 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; } 
0
Aug 08 '16 at 7:02
source share

PHP 51Degrees solution can do this. you can get a 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.

Due to the nature of Apple user agents, the original result will return a universal device, however, if you are interested in a specific device, you can use the JavaScript JavaScript client override to define a specific model.

To do this, you can implement something similar to the following logic, as soon as you determine that this is an Apple device, in this case for the 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 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 about research done on Apple devices, you can read their blog post here https://51degrees.com/blog/device-detection-for-apple-iphone-and-ipad .

Disclosure: I work for 51Degrees.

0
Apr 12 '17 at 9:48 on
source share

In response to the code of Chaim Evgi I added! == false to the end so that it works for me

 $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; 
0
Dec 12 '17 at 16:47
source share



All Articles