Check If PHP-Page Is Accessed from an iOS Device

Check if PHP-page is accessed from an iOS device

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

Simplest way to detect a mobile device in PHP

Here is a source:

  • Detect Mobile Browser
  • Download PHP script

Code:

<?php

$useragent=$_SERVER['HTTP_USER_AGENT'];

if(preg_match('/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i',$useragent)||preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i',substr($useragent,0,4)))

header('Location: http://detectmobilebrowser.com/mobile');

?>

PHP - Check if the page run on Mobile or Desktop browser

There is a very nice PHP library for detecting mobile clients here: http://mobiledetect.net

Using that it's quite easy to only display content for a mobile:

include 'Mobile_Detect.php';
$detect = new Mobile_Detect();

// Check for any mobile device.
if ($detect->isMobile()){
// mobile content
}
else {
// other content for desktops
}

determine iOS device type

jsFiddle DEMO ← Visit with your mobile device!

Remove /show/ in URL Address Bar to access the jsFiddle edit page.

http://detectmobilebrowsers.com/

The above open source method, DetectMobileBrowers plugin, contains the source you'll need, in a variety of formats, to quickly identify the browser and mobile devices including iphone / ipod.

The homepage above is also a Live Demo to check out which device / useragent your using.

Also, if you do use that script note Boolean true or false on the variable is to be used (i.e., use !! before variable name to test for false).


EDIT: The Supported Devices Section contains a downloadable archive of all devices and user agents detected. Mentioned there is a mod to include tablet support (e.g., iPad Tablets).

Sample Listing:

apple_generic_iphoneos_mmsclient
apple_ipad_ver1
apple_ipad_ver1_sub42
apple_ipad_ver1_sub43
apple_ipad_ver1_sub431
apple_ipad_ver1_sub5312110
apple_ipad_ver1_subiprod
apple_ipad_ver1_subsimulator
apple_iphone_coremedia_ver1
apple_iphone_coremedia_ver1_1_5
apple_iphone_coremedia_ver1_sub5a347
apple_iphone_coremedia_ver1_sub5b108
apple_iphone_coremedia_ver1_sub5f136
apple_iphone_coremedia_ver1_sub5f137
apple_iphone_coremedia_ver2_1_1
apple_iphone_coremedia_ver2_2
apple_iphone_coremedia_ver2_2_1
apple_iphone_coremedia_ver2_2a
apple_iphone_emulator_ver2
apple_iphone_emulator_ver2_1
apple_iphone_emulator_ver2_2_1
apple_iphone_emulator_ver2_sub52520
apple_iphone_emulator_ver2_sub52520_5f135
apple_iphone_emulator_ver3
apple_iphone_emulator_ver3_1
apple_iphone_emulator_ver4
apple_iphone_emulator_ver5
apple_iphone_ver1
apple_iphone_ver1_china
apple_iphone_ver1_os201
apple_iphone_ver1_somesdk
apple_iphone_ver1_somesdk_subenus
apple_iphone_ver1_sub1a542a
apple_iphone_ver1_sub1c25
apple_iphone_ver1_sub1c28
apple_iphone_ver1_sub3a109a_dadk
apple_iphone_ver1_sub3a109a_plpl
apple_iphone_ver1_sub3a109a_svse
apple_iphone_ver1_sub3a109a_zhcn
apple_iphone_ver1_sub3b48a
apple_iphone_ver1_sub3b48b
apple_iphone_ver1_sub3b48b_nlnl
apple_iphone_ver1_sub3b48b_ptpt
apple_iphone_ver1_sub3b48b_trtr
apple_iphone_ver1_sub3b48b_zhtw
apple_iphone_ver1_suba543
apple_iphone_ver1_suboperamini5
apple_iphone_ver2
apple_iphone_ver2_0_2
apple_iphone_ver2_0_2_subua
apple_iphone_ver2_1
apple_iphone_ver2_1_1
apple_iphone_ver2_1_sub525181
apple_iphone_ver2_1_sub525181_subua
apple_iphone_ver2_1_subcydia
apple_iphone_ver2_2
apple_iphone_ver2_2_1
apple_iphone_ver2_2_1_svse
apple_iphone_ver2_2_sub525181
apple_iphone_ver2_2_sub5g77
apple_iphone_ver2_2_subcydia
apple_iphone_ver2_2_subua
apple_iphone_ver2_sub525181
apple_iphone_ver2_sub5a347
apple_iphone_ver2_sub5b108
apple_iphone_ver3
apple_iphone_ver3_0_1
apple_iphone_ver3_1
apple_iphone_ver3_1_0
apple_iphone_ver3_1_0_sub52816
apple_iphone_ver3_1_1
apple_iphone_ver3_1_2
apple_iphone_ver3_1_2_svse
apple_iphone_ver3_1_3
apple_iphone_ver3_1_3_subenus
apple_iphone_ver3_1_sub7c116a
apple_iphone_ver3_sub7a259g
apple_iphone_ver3_sub7a312g
apple_iphone_ver3_sub7a341
apple_iphone_ver3_sub7a341_enus
apple_iphone_ver4
apple_iphone_ver4_1
apple_iphone_ver4_1_sub8b113
apple_iphone_ver4_1_sub8b117
apple_iphone_ver4_1_sub8b117_cydia
apple_iphone_ver4_1_subgoog_bot
apple_iphone_ver4_2
apple_iphone_ver4_2_1
apple_iphone_ver4_2_5
apple_iphone_ver4_2_6
apple_iphone_ver4_2_7
apple_iphone_ver4_2_8
apple_iphone_ver4_3
apple_iphone_ver4_3_1
apple_iphone_ver4_3_2
apple_iphone_ver4_3_3
apple_iphone_ver4_3_4
apple_iphone_ver4_3_5
apple_iphone_ver4_sub401
apple_iphone_ver4_sub405
apple_iphone_ver4_sub405_eses
apple_iphone_ver4_sub533179
apple_iphone_ver5
apple_iphone_ver5_1
apple_iphone_ver5_subua
apple_ipod_touch_ver1
apple_ipod_touch_ver1_fr
apple_ipod_touch_ver1_no_u
apple_ipod_touch_ver1_os201
apple_ipod_touch_ver1_os211
apple_ipod_touch_ver1_sub3b48b_dadk
apple_ipod_touch_ver1_sub3b48b_fr
apple_ipod_touch_ver1_sub3b48b_jajp
apple_ipod_touch_ver1_sub3b48b_kokr
apple_ipod_touch_ver1_sub3b48b_nlnl
apple_ipod_touch_ver1_sub3b48b_svse
apple_ipod_touch_ver1_sub3b48b_zhcn
apple_ipod_touch_ver1_sub3b48b_zhtw
apple_ipod_touch_ver1_sub4193
apple_ipod_touch_ver1_sub4a102
apple_ipod_touch_ver1_sub4a93_plpl
apple_ipod_touch_ver1_sub4a93_ptpt
apple_ipod_touch_ver1_sub4b1
apple_ipod_touch_ver1_subenus_sub5f137
apple_ipod_touch_ver2
apple_ipod_touch_ver2_1
apple_ipod_touch_ver2_1_engb
apple_ipod_touch_ver2_1_subenus_sub5f138
apple_ipod_touch_ver2_2
apple_ipod_touch_ver2_2_1
apple_ipod_touch_ver2_2_1_sub5h11
apple_ipod_touch_ver2_2_1_sub5h11a
apple_ipod_touch_ver2_2_1_subengb
apple_ipod_touch_ver2_2_sub5g77a
apple_ipod_touch_ver2_sub525181
apple_ipod_touch_ver2_sub5a347
apple_ipod_touch_ver2_sub5c1
apple_ipod_touch_ver3
apple_ipod_touch_ver3_1
apple_ipod_touch_ver3_1_1
apple_ipod_touch_ver3_1_1_subua
apple_ipod_touch_ver3_1_2
apple_ipod_touch_ver3_1_2_sub7d11
apple_ipod_touch_ver3_1_2_subua
apple_ipod_touch_ver3_1_3
apple_ipod_touch_ver3_1_3_subua
apple_ipod_touch_ver3_1_subua
apple_ipod_touch_ver3_subua
apple_ipod_touch_ver4
apple_ipod_touch_ver4_1
apple_ipod_touch_ver4_1_subua
apple_ipod_touch_ver4_2_1
apple_ipod_touch_ver4_2_1_subua
apple_ipod_touch_ver4_3
apple_ipod_touch_ver4_3_1
apple_ipod_touch_ver4_3_1_subua
apple_ipod_touch_ver4_3_2
apple_ipod_touch_ver4_3_2_subua
apple_ipod_touch_ver4_3_3
apple_ipod_touch_ver4_3_3_subua
apple_ipod_touch_ver4_3_4
apple_ipod_touch_ver4_3_5
apple_ipod_touch_ver4_3_subua
apple_ipod_touch_ver4_sub402
apple_ipod_touch_ver4_sub402ua
apple_ipod_touch_ver4_subua
apple_ipod_touch_ver5
apple_ipod_ver1

After Accepted Answer Status:
As an alternate to DetectMobileBrowsers plugin, the other SO Answer on this page by inhan for The MobileESP Project does something similar. For future visitors to this Question, check them both out. Cheers!

Get Specific Device Information

There is a really simple solution to use Mobile-detect here: https://github.com/serbanghita/Mobile-Detect/

Enjoy!

Detect iOS version with PHP

The HTTP_USER_AGENT will return the following:

Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7

If you are trying to detect iOS 5, do the following:

 <?php if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone OS 5_0') !== false) { };?>


Related Topics



Leave a reply



Submit