UserAgent parsing with PHP for Browser, Operating system, mobile, tablet detection

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

 

  1. <?php
  2.  
  3. require("UAParser.php");
  4. // lets load our uaObject with USER_AGENT parsed data
  5. $usObject = UA::parse();
  6.  
  7. // lets get the browser
  8. echo $usObject->browserFull;
  9. // -> "Chrome 20.0.1132"
  10. // just the browser name.
  11. echo $usObject->browser;
  12. // -> "Chrome"
  13.  
  14. echo $usObject->version;
  15. // -> "20.0.1132"
  16.  
  17. // this is major version of the browser. May not be used that often
  18. echo $usObject->major;
  19. // -> 20
  20.  
  21. // extract the Operating system
  22. echo $usObject->os;
  23. // -> "Windows 7"
  24.  
  25. ?>

 

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

  1.   $usObject->device; // will return device name in selected cases
Just in case you want to detect if the device is Mobile, Tablet use this
  1. $usObject->isMobile; // return bool if the user is using mobile browser
  2. $usObject->isMobileDevice; // return true if its a mobile device
  3. $usObject->isTablet; // return true if requesting device is a tablet
  4. $usObject->isComputer; // return true if device is a computer as per USER_AGENT info

User-Agent for my tests

  1. 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

  1. require("UAParser.php");
  2.  
  3. // lets parse the user agent information
  4. $uaObject = UA::parse();
  5.  
  6. // lets redirect to my mobile site if isMobile returns true
  7. if ($uaObject->isMobile) {
  8. header("location:http://jaspreetchahal.org");
  9. }
  10. // 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
  11. if ($uaObject->isMobile) {
  12. echo '<link rel="stylesheet" type="text/css" media="screen,print" href="mobile.css">';
  13. }
  14. else {
  15. echo '<link rel="stylesheet" type="text/css" media="screen,print" href="desktop.css">';
  16. }

Getting YAML files

Just in case the script asks you for missing  YAML files do this (change the paths)

ua-parser get YAML

 

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

 

 

 

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Comments

  1. Wherer is the “UAParser.php” file located?
    Please provide this file.
    thanks

Speak Your Mind

*

CommentLuv badge