Check if PHP-page is accessed from an iOS device
114,437
Solution 1
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
Solution 2
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;
}
Solution 3
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';
}
}
Solution 4
$browser = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
Solution 5
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;
}
Author by
Snilleblixten
Photographer in Paris, trying to learn to create apps.
Updated on July 08, 2022Comments
-
Snilleblixten almost 2 years
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?
-
Snilleblixten almost 13 yearsBut it was not really true, I found out. Those headers are just from Safari in the phone. If you connect from an app, it looks something like this: app%20name/1.0 CFNetwork/485.13.8 Darwin/11.0.0 . So maybe I can look for CFNetwork or Darwin instead, or the app name.
-
Asad Khan about 12 yearsNot working for me I am getting 1.1 CFNetwork/548.0.3 Darwin/11.0.0 for both iPhone and iPad :S now how should I differentiate between them ???
-
Kishor Kundan over 11 years@AsadKhan wore u testing from the simulator ? I am getting same Http_user_agent
-
Kishor Kundan over 11 years@Snilleblixten did it work out ? m stuck with similar situation.
-
gkd almost 9 yearsMozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/43.0.2357.130 Chrome/43.0.2357.130 Safari/537.36 ==> tat was my user agent for chromium browser Version 43.0.2357.130 Ubuntu 14.04 (64-bit). - $webOS will not set to value >= 0
-
Thomas Kekeisen over 7 yearsYou should add a
!empty($_SERVER['HTTP_USER_AGENT'])
since$_SERVER['HTTP_USER_AGENT']
is not set when the client sends no user agent at all. Your function will crash then/show a notice. -
Brian C almost 6 yearsBest to add this as a comment on his answer, even though I know it's some time since you commented. Thanks for the code!
-
Gloson almost 6 years@BrianC Sigh, Stack Overflow says I must have 50 reputation to comment.
-
zvi almost 5 yearsFrom iOS 13 the iPad's user agent has changed to Mac OS, for example:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Safari/605.1.15
... -
Fadi over 4 yearsupdate for new: $iPadOS = stripos($_SERVER['HTTP_USER_AGENT'],"Macintosh");
-
Rodrigo Zuluaga almost 4 yearsWorking perfect. Tested on 2 iphones, Ipad. Thanks its 2020!!
-
drooh over 3 yearsWill detecting "Macintosh" only be for new iPads or will that include Mac laptops & desktops?