[php] 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?

This question is related to php iphone ios http-headers

The answer is


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

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;

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

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

It's work for Iphone

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

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

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

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

Or for an 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 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.


If you just want to detect mobile devices in generel, Cake has build in support using RequestHandler->isMobile() (http://book.cakephp.org/2.0/en/core-libraries/components/request-handling.html#RequestHandlerComponent::isMobile)


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to iphone

Detect if the device is iPhone X Xcode 8 shows error that provisioning profile doesn't include signing certificate Access files in /var/mobile/Containers/Data/Application without jailbreaking iPhone Certificate has either expired or has been revoked Missing Compliance in Status when I add built for internal testing in Test Flight.How to solve? cordova run with ios error .. Error code 65 for command: xcodebuild with args: "Could not find Developer Disk Image" Reason: no suitable image found iPad Multitasking support requires these orientations How to insert new cell into UITableView in Swift

Examples related to ios

Adding a UISegmentedControl to UITableView Crop image to specified size and picture location Undefined Symbols error when integrating Apptentive iOS SDK via Cocoapods Keep placeholder text in UITextField on input in IOS Accessing AppDelegate from framework? Autoresize View When SubViews are Added Warp \ bend effect on a UIView? Speech input for visually impaired users without the need to tap the screen make UITableViewCell selectable only while editing Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture arm64

Examples related to http-headers

Set cookies for cross origin requests Adding a HTTP header to the Angular HttpClient doesn't send the header, why? Passing headers with axios POST request What is HTTP "Host" header? CORS error :Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response Using Axios GET with Authorization Header in React-Native App Axios get access to response header fields Custom header to HttpClient request Send multipart/form-data files with angular using $http Best HTTP Authorization header type for JWT