Parse the user agent with ua-parser in php – detect browser, os and device
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 urlhttps://github.com/tobie/ua-parser
ua-parser is available for many languages including php. Usage in php is quite simple
require("uaparser.php"); $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 ipmango.com.