Php if ios device

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?

Php Solutions

Solution 1 — Php

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

 //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 — Php

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 — Php

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 — Php

$browser = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone"); 

Solution 5 — Php

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; > 

Solution 6 — Php

Solution 7 — Php

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

Solution 8 — Php

 $iPhone = false; $AndroidPhone = false; $deviceType = 0; $ua = strtolower($_SERVER['HTTP_USER_AGENT']); print "
"
.$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; > ?>

Solution 9 — Php

51Degrees’ PHP solution is able to do this. you can get the 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’s User-Agents the initial result will return a generic device, however if you are interested in the specific device you can use a JavaScript client side override to determine to specific model.

To do this you can implement something similar to the following logic once you have determined it is an Apple Device, in this case for an 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"; >; > 
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 on research 51Degrees have done into 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.

Solution 10 — Php

In response to Haim Evgi’s code, I added !== false to the end for it to work 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; 

Источник

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?

Php Solutions

Solution 1 — Php

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

 //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 — Php

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 — Php

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 — Php

$browser = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone"); 

Solution 5 — Php

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; > 

Solution 6 — Php

Solution 7 — Php

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

Solution 8 — Php

 $iPhone = false; $AndroidPhone = false; $deviceType = 0; $ua = strtolower($_SERVER['HTTP_USER_AGENT']); print "
"
.$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; > ?>

Solution 9 — Php

51Degrees’ PHP solution is able to do this. you can get the 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’s User-Agents the initial result will return a generic device, however if you are interested in the specific device you can use a JavaScript client side override to determine to specific model.

To do this you can implement something similar to the following logic once you have determined it is an Apple Device, in this case for an 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"; >; > 
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 on research 51Degrees have done into 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.

Solution 10 — Php

In response to Haim Evgi’s code, I added !== false to the end for it to work 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; 

Источник

Check if PHP-page is accessed from an iOS device

If you want to determine whether a PHP page is being accessed from an iOS device, you can use PHP’s `$_SERVER[‘HTTP_USER_AGENT’]` variable. This variable contains information about the user agent (web browser or other client software) that is accessing the PHP page.

To check if the user agent is from an iOS device, you can use the `strpos()` function to search for the string “iPhone” or “iPad” within the `$_SERVER[‘HTTP_USER_AGENT’]` variable.

Here is an example code snippet that demonstrates how to check if a PHP page is being accessed from an iOS device:

$is_ios = false; $user_agent = $_SERVER['HTTP_USER_AGENT']; if (strpos($user_agent, 'iPhone') !== false || strpos($user_agent, 'iPad') !== false) < $is_ios = true; >if ($is_ios) < // Do something specific for iOS devices >else < // Do something else for non-iOS devices >

In this code, we first set the `$is_ios` variable to `false`. We then get the user agent from the `$_SERVER[‘HTTP_USER_AGENT’]` variable and store it in the `$user_agent` variable.

We then use the `strpos()` function to search for the strings “iPhone” and “iPad” within the `$user_agent` variable. If either of these strings is found, the `$is_ios` variable is set to `true`.

We can then use the `$is_ios` variable to determine whether the user agent is from an iOS device or not, and perform different actions accordingly.

This simple code can be very useful if you need to provide different content or functionality for iOS users on your PHP website or application.

Источник

Читайте также:  Javascript clear all input
Оцените статью