Hi Guys,
While working on this project lately I realized that I had this requirement with I have to detect User browser and their operating system.
All this information is present in request header USER_AGENT.
Well you can detect what user is using to browse your website.
Now while working on this requirement, I thought maybe its a wise thing to search around if something really cool exist other than getBrowser function in PHP.
I was glad that I found
https://github.com/tobie/ua-parser
This library also have extensions for Javascript and Python just in case you want to use those. But in this post I am touching functionality available for PHP.
Alright so when you extract the downloaded archive or if you git clone the above repo, you will see that there is a directory called php that is where file UAParser.php exists.
Here is a code to extract some really good info from USER_AGENT header
Advertisement
<?php require("UAParser.php"); // lets load our uaObject with USER_AGENT parsed data $usObject = UA::parse(); // lets get the browser echo $usObject->browserFull; // -> "Chrome 20.0.1132" // just the browser name. echo $usObject->browser; // -> "Chrome" echo $usObject->version; // -> "20.0.1132" // this is major version of the browser. May not be used that often echo $usObject->major; // -> 20 // extract the Operating system echo $usObject->os; // -> "Windows 7" ?>
The above function is just a hint of this powerful library. It does not stop here.
There are other very helpful functions available too like for example
Just in case you want to detect if the device is Mobile, Tablet use this
$usObject->device; // will return device name in selected cases
$usObject->isMobile; // return bool if the user is using mobile browser $usObject->isMobileDevice; // return true if its a mobile device $usObject->isTablet; // return true if requesting device is a tablet $usObject->isComputer; // return true if device is a computer as per USER_AGENT info
User-Agent for my tests
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11
Usage example
Say you have different themes for your website one for mobile and another for desktop browsers or you have 2 different sites for each of them. You can do something like this
require("UAParser.php"); // lets parse the user agent information $uaObject = UA::parse(); // lets redirect to my mobile site if isMobile returns true if ($uaObject->isMobile) { } // above if condition redirects the condition below load the responsive CSS themes. This is just an example. you should use it as per your requirement if ($uaObject->isMobile) { echo '<link rel="stylesheet" type="text/css" media="screen,print" href="mobile.css">'; } else { echo '<link rel="stylesheet" type="text/css" media="screen,print" href="desktop.css">'; }
Getting YAML files
Just in case the script asks you for missing YAML files do this (change the paths)

Demo
http://jaspreetchahal.org/examples/ua-parser-test.php
You can do all of the above with Javascript and Python nodes too that come with above library.
I hope that I have moved you in the right direction if detecting a browser, device, operating system etc is on the list of requirements that you are working on now.
Cheers
Advertisement





JC WordPress Coupon Revealer Plugin Pro License
Australian Street Names with City, State and Display Names only, Single Server License
Wherer is the “UAParser.php” file located?
Please provide this file.
thanks
You can download full package from the URL I mentioned in the post. Here is the URL once again
https://github.com/tobie/ua-parser
Cheers!