3

For detecting Internet Explorer I use his line.

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

How do I detect iOS 5.

Walrus
  • 19,801
  • 35
  • 121
  • 199
  • Check out: http://www.useragentstring.com/pages/Browserlist/ for a list of user agent strings – PeeHaa Oct 14 '11 at 13:32

4 Answers4

7

I had a similar problem but needed to get the version number in iPhone and iPad using $_SERVER['HTTP_USER_AGENT']. I suppose it is not completely reliable, but it worked for me:

<?php 

$version = preg_replace("/(.*) OS ([0-9]*)_(.*)/","$2", $_SERVER['HTTP_USER_AGENT']);

// for example you use it this way

if ($version > 5){
// do something
}


?> 
Jorge
  • 831
  • 13
  • 18
  • I found that some user agents were not detected properly and returned as Mac OS X for the OS instead of iPad iOS (user agent detection probably needs updated) so I used this preg_match variant: preg_match( "/(.*) OS ?X? ([0-9]*)_(.*)/", $user_agent, $matches ); – hanmari Aug 31 '20 at 17:37
5

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) { };?>
Semere Taézaz Sium
  • 4,036
  • 3
  • 21
  • 26
0

If this is possible, get_browser() will be the way to do it.

However, since a User-Agent string is easily spoofable, and the list of browsers/OSes is a constantly moving target, this will never be 100% reliable. In order for this to work at all, you must keep the php_browscap.ini file up to date (see note on the linked manual page).

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • Would you mind giving a bit more detail with my if else equation on what user agent I am looking for with iOS – Walrus Oct 14 '11 at 13:55
  • I can't, because I don't own an iOS device to test with. Sorry. You would have to test it yourself (just `var_dump(get_browser(null,true));`) – DaveRandom Oct 14 '11 at 14:00
0

Simple and easy

<?php
   $browser = get_browser(null, true);
   echo $browser['platform'];
?> 

The docs say

Note:

In order for this to work, your browscap configuration setting in php.ini must point to the correct location of the browscap.ini file on your system.

browscap.ini is not bundled with PHP, but you may find an up-to-date » php_browscap.ini file here.

While browscap.ini contains information on many browsers, it relies on user updates to keep the database current. The format of the file is fairly self-explanatory.

Mob
  • 10,958
  • 6
  • 41
  • 58