Site Information

W3C Validation:

XHTML CSS

Site hosted by:

pair
networks

Scripts — Full Featured PHP Browser Detection — Changes

These are the changes that might affect your implementation of this script. Please take a moment to review them to make sure you are taking advantage of all the features of this script. Also of course check the script comment headers for most up to date feature and option list.

If you need to test your code or site, use Chris Pederick's Firefox User-Agent Switcher Extension along with our import User-Agent List (download xml file).

Script Download

Download: complex, full featured PHP Browser Detection
Current Version: 5.4.2
File Last Modified: December 28, 2011. 14:05:00 pm

Script Update History

Version 5.4.2
Added explicit Blackberry OS version support

That's all for this version, by request, now you can get the specific Blackberry OS version number. Handles old and new format for Blackberry UA strings.

Version 5.4.1
Fixed small bug, bad id for CoolWebProducts IE

The search string webpro made browsers infected with the coolwebpro semi-spyware on MSIE show as mobile device. Solution: change search string to webpro/

Nothing else, no great excitement, but a real bug, and a real bug fix.

Version 5.4.0
New return values for layout engine and html5 support

By request, added a new return value, html_type, that returns an integer for html 5 support level. 1 is basic HTML, no real HTML 5 support. 2 is basic HTML 5 support. 3 is Basic plus HTML Forms. Please note, this is based on Wikipedia Layout Engine Support Page and should NEVER be used in place of proper testing of methods, although you can use it for a quick and dirty tag/element selection. Read the top comments for more explanation, and make sure to also check the wikipedia page. These are subjective judgements on my part based on the support levels of each browser engine, not absolute.

Also added, as required by that new feature, a return array: engine_data, which, if there is an engine detected in the string, returns: 0-engine name; 1-full engine version number string; 2-engine number, for math comparison. Keep in mind always that dot numbers must be further processed in your programming if you are in a locale that uses commas as decimal separators.

Currently supported layout engines for that feature are: Trident (MSIE 7 or greater); Presto (Opera 10 or greater); Gecko; Webkit. Note that webkit engine numbers are not always exactly the same as for example Safari numbers, though they are similar, and are not at all the same as for example Chrome numbers. Layout engine support is based on the presence of a string id of that engine in the UserAgent.

Note that the "Your Computer Box" script now uses the engine_data array to show layout engine data, if available.

Version 5.3.18
Added new return value for mobile data: mobile_tablet

By request, added return value to the main mobile data array of mobile tablet. This is array key 8, and will be either null or not null, so test for null, not true/false. Or just do:

if ( $a_browser_data['mobile_data'][8] ){ do tablet stuff }

Version 5.3.17
Shadowfox added to moz_types

Tiny change, added shadowfox to sub mozilla types array.

Version 5.3.16
Support for MSIE 10, 11 standard/compatibility mode; new Webkit browsers.

Since Microsoft is again apparently doing active, ongoing, browser development, I expect the compatibility mode method to be used on 11 as well. If it's not, that won't matter, since the flag will simply not be triggered.

This will save you a future update when MSIE 11 comes out. Here's the latest MSIE Dev Blog on the MSIE 10 UserAgent.

See the notes for 5.3.12 changes for more information about the standards/compatability mode.

Also added a few new webkit running browsers, icab, maxthon, rekonq, and shiira.

Version 5.3.15
New Feature: Optional third script parameter for testing different UA strings. Also added bing/ exabot/ google web preview to bot list

Optional third script parameter, pass the script externally derived UA strings, for testing/processing purposes. Idea from Rui Teixeira.
Note: include a blank second arg when you use the 3rd parameter if the second is not set:
example: browser_detection( 'full', '', $test_string_data )

If you use this parameter, the script will do the full processing on the UA string you have passed it via the function call, then switch $b_repeat back to true at the end.

However, be aware that all requests to the script after the last testing value is sent will use the previous testing value, NOT the actual UA string, so make sure to handle that in your programming if you require the true UA data to be processed after the final testing value is sent by resetting that data with the true UA value.

No change to your existing programming required since the third parameter is optional and can be null.

Version 5.3.14
Firefox Minefield moz type added

Added support to test for alpha Firefox releases, using Minefield, not Firefox string. This can be useful for testing Linux Firefox 64 bit support.

Version 5.3.13
New return value: ie9x

Because MSIE 9 is finally supporting real web standards, it seemed reasonable to add: ie9x as a new return value for ie_version.

Keep in mind however that if the user has MSIE 9 in compatibility mode, ie_version will be ie7x, not 9x for MSIE 9.

Version 5.3.12
MSIE 9 standard/compat modes

Handles new MSIE 9 user agent syntax:
standard: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
compat mode: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/5.0)

Note that MSIE 9 syntax has changed: standard, Mozilla/5.0, not 4.0, and Trident/5.0, not 4.0. But I'm glad that Microsoft finally is supporting real web standards, that's a welcome change.

These user agent types were also added to the import User-Agent List v. 2.1.5 (download xml file) for your testing convenience.

Version 5.3.11
iPad detection added

Added iPad device detection. Also added 'cpu os' as mobile OS type, that is actually how iPad OS X identifies itself, if you want to treat this for output to say for example 'iPad OS' then you'll need to do that in your output programming.

Given that iPad has a larger screen, you'll probably want to be handling iPad differently than iPhone/iPod/Droid/Blackberry type devices.

These user agent types were also added to the import User-Agent List v. 2.1.4 (download xml file) for your testing convenience.

Version 5.3.10
New HTC Devices

Added explicit detection of the following: HTC Espresso, HTC Huangshan, HTC Liberty, HTC Halo, HTC Tattoo, HTC Paradise, HTC Legend, HTC Supersonic.

Not much else, added a wildcard 'htc ' to catch new devices as well.

Version 5.3.9
Added Windows Phone OS / Zune(hd) support

Simple update, mobile detection now supports the coming Windows Phone OS, unless Microsoft changes the user agent string, and also added support for zune/zunehd devices, mobile again.

These user agent types were also added to the import User-Agent List v. 2.1.3 (download xml file) for your testing convenience.

Version 5.3.7
Fixed Opera Version for > 9.80

Opera has decided to freeze their version numbers to 9.8x (or 9.9x) for all future releases due to poorly written browser sniffers (not this one, of course!!).

The browser detector now handles that, and returns the correct version, for example 10.10.

Version 5.3.3
New Mobile Device Data Type: $version_number

This adds array key 7 to the $mobile_data array, and returns, if found, the version number of the mobile device, for example:
Device Type: blackberry v: 9700

Also added in actual OS X version number, like 10.4.3, return. Defaults to 10 for OS X for $os_number, but if the more exact number is found in the user agent, it will return that instead.

If you are wanting to do math comparisons, simply process the number to make it decimal instead of two dot:
sprintf("%02.1f", $os_number );
will return either 10.0 or 10.4, which can then be compared with arithmetic.

Version 5.3.2
script execution time parameter: run_time

If you want to test what the various second parameter switches do (hint, they make a huge difference) in terms of general script run time, you can now get this information (PHP 5 or greater required, script will test and return null if old PHP).

Here's a sample, using the 'full' data return:
browser_detection('full', '') = 0.00116205
browser_detection('full', '1') = 0.00053501
browser_detection('full', '2') = 0.00043797
browser_detection('full', '3') = 0.00029707

Available as a standalone parameter, 'run_time', as full associative array key: 'run_time', and as 'full' array key 15.

Note that the timer resets itself every request, and since the data arrays are loaded the first run through, the next run throughs are very fast, eg: 0.00005007 seconds.

Version 5.3.0
Simplified and Standardized parameter values.

I redid the entire script to use standard parameter value names/variable names, to make it more obvious.

NOTE: You do not need to change your working code. The script will update the old deprecated parameter values to the new ones internally, so the new script will not break your working code.

See the top comment header in the script for full explanation and new/old names. If you feel like it, change the parameter names to keep it consistent, but these names will always be translated by the script, so you don't have to change anything.

Version 5.2.2
New return value: full_assoc

silverkitty sent in this patch to add associative array full output: full_assoc

The array keys are as follows (but check latest script version, this page doesn't always get updated quickly). (UPDATE: Note the altered key names)

'browser_working' => $browser_working,
'browser_number' => $browser_number,
'ie_version' => $ie_version,
'dom' => $b_dom_browser,
'safe' => $b_safe_browser,
'os' => $os_type,
'os_number' => $os_number,
'browser_name_' => $browser_name,
'ua_type' => $ua_type,
'browser_math_number' => $browser_math_number,
'moz_data' => $a_moz_data,
'webkit_data' => $a_webkit_data,
'mobile_test' => $mobile_test,
'mobile_data' => $a_mobile_data,
'true_ie_number' => $true_ie_number,
'run_time' => $run_time
Version 5.2.0
New return value: true_ie_number

This data is also returned in a new full return array item, index 14 ($true_ie_number). Since MSIE 8.0 is using a compatability mode which changes the user agent string to say it's MSIE 7, this will detect that change and offer the true version. Only for MSIE 8 masquerading as MSIE 7.

This is also now integrated into the latest Your Computer Box script version, as you can see if you turn MSIE 8 compatibility mode on. The box to your left should then show both the real version and the compatibility mode version.

Also a major cleanup of comments, clarification of top comment descriptions of return data to make it a lot more readable for new (and old) users.

Version 5.1.0
With New Arguments: Optimization Parameters

New optional argument, second primary function parameter with possible values: 1 2 3
1 turns off os detection functions, 2 turns off mobile detection functions, and 3 turns off both. Leaving argument 2 blank leaves it default. This can help cut script execution times down quite a bit if you don't need os and / or mobile data.

Version 5.0.4
With Mobile Detection Released! Read Changes Below

I expect fine tuning and adding more handheld/mobile data to this over time, but I think this should get probably more than 95% of current devices reasonably well. We'll see how it goes.

Please note that if you use the your computer script, you need to update it to the current version, there will be a failure on mozilla and webkit output unless you change those lines, or update the script.

Many thanks to www.zytrax.com for their invaluable list of UA strings for handheld and other weird devices.

Updated mobile_data array values :: v. 5.0.4

To avoid breaking this in the future, I redid the return values, and added one new type:
$mobile_device, $mobile_browser, $mobile_browser_number, $mobile_os, $mobile_os_number, $mobile_server, $mobile_server_number

This will break earlier version of your computer info box script if you use that. However, this change was worth making because it is now more consistent and I can add another identification category in the future without breaking the order of the array items. The only future change will be in adding possible either methods for id'ing mobile devices. $mobile_server is somethign like CTS, Novarra-Vision for example, or DoCoMo.

Also a bug fix in this one, and added some more id methods. I expect this to change over time as user feedback exposes errors and failures, but from now on the actual array order should not change, although the items themselves might get shuffled around to be more accurate.

New Return Values

To handle mobile detections: new parameters: 'mobile_test' and 'mobile_data'. Test returns a string or null if it's a detected mobile type. Data returs an array of mobile data or null if it's not a mobile type.

To handle the new Webkit parameter, new option: 'webkit_data' added. Also added 'moz_data' and 'webkit_data' return arrays to full return array so you don't have to call browser detection twice in case of Mozilla or Webkit browsers.

This should not change any existing code for you, they are just new extra items. But if you want to check your code to see if this can slightly optimize your pages, then that might be worth it if you use either Mozilla or Webkit in the page handling code.

Mobile Detection

By popular demand. Please note that there are a few changes now. 'full' now returns 'mobile_test' and 'mobile_data' as the last array items. Os type has: 'android', 'blackberry', 'iphone', 'palmos', 'palmsource', 'symbian' added to basic tests. Windows is still going to return the ce as version number for windows ce, but you should consider that deprecated and test for 'windows ce' in the mobile_test string instead if you need that.

'mobile_test' always runs, and returns either in 'full' array data, or as its own test. This will just be a string of however the script found the mobile. There will probably be bugs with this. 'mobile_data' is an array with these items: $mobile_device, $mobile_browser, $mobile_browser_number, $mobile_os, $mobile_os_number, $mobile_server, $mobile_server_number. Please note that the numbers are going to be best guesses only, and will encure some script overhead to get, since they involved resending data through functions.

The 'ua_type' parameter also now returns 'mobile' as well as 'bro', 'bbro', and 'bot' to make testing easier for hand held/mobile devices.

iPhone/hand held OS Detection

iPhone should not have been in the browser detection, that was a mistake, it is now in the os detection, with the other primary handheld os systems: 'android', 'blackberry', 'iphone', 'palmos', 'palmsource', 'symbian'.

For more precise handheld device detection use the basic 'mobile_test' and the full test array 'mobile_data' options instead. The 'ua_type' parameter now also has 'mobile' as a return value for easy testing.

WebKit Detection

WebKit handling: now detects and returns as browser type array: Safari, Epiphany (webkit build), Google Chrome, Omniweb (and more), with Webkit type version number (ie, Chrome 1.2), and the Webkit number (ie Webkit/442).

Please note that this adds a new return parameter, webkit_data, which returns an array of the webkit data (specific browser name and version number, plus webkit version number). The iPhone detection was removed from this and put into OS and Mobile parameters where it belonged.

Pre Version 5.0.0
Updates and Changes

Unix / Linux Version Detection

Linux version detection. Since most Linux browsers don't have much about the Linux version in the useragent string, this is pretty basic, but added anyway.

Netscape 2,3,4 detection

The script has been changed to actually id the old netscape browsers correctly, for those who who really want the most accurate. This can be useful for old Unix / Mac computers that still run these original Netscape browsers and can't update.

Important: If you are updating your script and were using the old 'ns4' to test for Netscape 4 you will have to modify your page code to test for 'ns' and for 'number' just like you do with Opera and Internet Explorer.


Please Link to our Site

It would really help us out, and help other people find this information, if you could add a link to our site in exchange for these scripts. Thanks.

<a href="http://techpatterns.com/downloads/php_browser_detection.php"> PHP Browser Detection</a>

top

More Information

This is your browser useragent string:

CCBot/1.0 (+http://www.commoncrawl.org/bot.html)

Manual Downloads

If for some reason the above download links didn't work, you can download these scripts here:


PHP browser detector Bug Alerts

Small bug in 5.3.3 that briefly broke some browser version number returns. This is fixed in 5.3.4.

Bug in versions less than 4.11.2: Array count error in some cases. Please download the latest version. It might affect your site at some point. Thanks to privacygirl.com for finding this.

Thanks to the Dan Nielson for pointing out a bug in our Linux detection, that's fixed as of version 4.9.6. The bug made gentoo and ubuntu id as Windows NT, sorry about that.

Thanks to the OmniWeb team for pointing out a bug in our OmniWeb detection, that's fixed as of version 4.9.4. Update: now returns array of webkit data when required (verson 4.11.0 and later).

Thanks to Bernward Ferber for finding a bug in the ie_version detection, that\'s fixed as of version 4.9.2. This bug only affected the script in the case of generic Mac IE detection.

IMPORTANT:

Unix Version Detection: There was a serious bug in the unix detection script. That is fixed as of version 4.4.3.

Mac OS X detector: If you downloaded any version pre 4.4.1 with Mac OS X detection there was a serious bug in the script.

Please update your scripts or you may get parse errors if a Mac/Unix/Spider visits your site that will prevent the page from being rendered.

Download Notes

Generic programs like MS Notepad will not open these downloaded text files correctly.

Please use your text editor (for example, our personal favorite, EditPlus, or HTML-Kit, BB edit, etc. to open them.

If you just want to take a look at them in your browser, go to the bottom of the page and click the Manual Download links.