3

For detecting Internet Explorer I use his line.

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

How do I detect iOS 5.

1

4 Answers 4

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
}


?> 
Sign up to request clarification or add additional context in comments.

1 Comment

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

3 Comments

Im sure you are correct. Icannot test it at the moment. Thanks
Out of curiosity what if I wanted to check for multiple user agents
If you want to check for multiple user agents, then yes, get_browser() is the way to go. Just pay close attention to the note about keeping php_browscap.ini up to date.
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).

2 Comments

Would you mind giving a bit more detail with my if else equation on what user agent I am looking for with iOS
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));)
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.