Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if PHP-page is accessed from an iOS device

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?

like image 552
Snilleblixten Avatar asked Jun 12 '11 13:06

Snilleblixten


People also ask

How to detect iOS device in PHP?

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

How do I check if a website is open in mobile or desktop PHP?

An easy way to detect a mobile or desktop device in PHP is to check if the HTTP user agent contains the word “mobile”. $ua = strtolower($_SERVER["HTTP_USER_AGENT"]); $isMob = is_numeric(strpos($ua, "mobile"));

How does iPad detect PHP?

You can sniff the iPad's user-agent header via $_SERVER['HTTP_USER_AGENT'] , but ideally, if you can feature-detect the things you want to be different on the iPad vs. any other device, that's more robust and flexible than agent sniffing.


1 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 details of the user device I recommended to use one of the following solutions: http://51degrees.mobi or http://deviceatlas.com

like image 94
Haim Evgi Avatar answered Oct 22 '22 01:10

Haim Evgi