Parse the user agent with ua-parser in php – detect browser, os and device

By | March 22, 2013

In your web application you might need to parse the user's user-agent string to find out the browser/OS/device being used. This is necessary specially when your webapp tries to adapt to the user's platform in a better way. For example a different layout for mobile device or restricted features/graceful degradation on a non supporting browser.

To do this you can analyse the user agent string in php which is found in the $_SERVER superglobal string. However there are so many types of user agents that you may not want to write your own library for doing the lengthy task. Better use a ready made library that does the job well. And its called ua-parser and can be downloaded from its github url
ua-parser is a multi-language port of BrowserScope's user agent string parser.

ua-parser is available for many languages including php. Usage in php is quite simple


$ua = "Mozilla/5.0 (Macintosh; Intel Ma...";

$parser = new UAParser;
$result = $parser->parse($ua);

print $result->ua->family;                // Safari
print $result->ua->major;                 // 6
print $result->ua->minor;                 // 0
print $result->ua->patch;                 // 2
print $result->ua->toString;              // Safari 6.0.2
print $result->ua->toVersionString;       // 6.0.2

print $result->os->family;                // Mac OS X
print $result->os->major;                 // 10
print $result->os->minor;                 // 7
print $result->os->patch;                 // 5
print $result->os->patch_minor;           // [null]
print $result->os->toString;              // Mac OS X 10.7.5
print $result->os->toVersionString;       // 10.7.5

print $result->device->family;            // Other

print $result->toFullString;              // Safari 6.0.2/Mac OS X 10.7.5

So for example if you are looking for a mobile device then check out the information available in "$result->device".
Similary browser and os information can also be found out.

Check the library in action at

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

One Comment

Parse the user agent with ua-parser in php – detect browser, os and device
  1. Pedro Perez Pereda


    but where is the uaparser.php? I get the repo from github but there is not uaparser.php… can you help me?


Leave a Reply

Your email address will not be published. Required fields are marked *