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 $browser = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone"); if ($browser == true){ $browser = 'iphone'; } ?>
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"));
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With