Detecting browser and browser capabilities with PHP

Detecting browsers and collecting data about browser capabilites can be done with Javascript or Activex, but there is still a long way away from being 100% accurate. There are thounsands of browser types to detect. In every update of IE/Firefox or etc. versions of the browsers are changing. At the time of this writing, there are more than 120.000 known browser UserAgents. From this perspective it can be said that collecting all the data about browsers for testing and detecting purposes is a quite challenging issue. Browscap browser database solves this problem.

What is browscap

Gary Keith, the lead developer of browscap defined explained browscap as follows:

Microsoft had a good idea when they created browscap.dll and browscap.ini to use with their web server for browser identification. In theory, someone at Microsoft would routinely update the browscap.ini file and make it available to its customers. As a result we would all have reliable browser identification in order to know exactly what features the client's browser supports. Sadly, Microsoft never made any attempt to keep the file up to date.

There was a brief wellspring of volunteers who did a good job of maintaining browscap.ini for a while. Those sources are now either intentionally out of date as a way to drive product sales, or no longer exist. I can't blame them for giving up as this is not an easy project to maintain.

Here is what I do as part of my project.

Shortly after midnight on Sunday morning the website log files for this site and over thirty other sites of various types are downloaded for analysis. Once downloaded the log files are analyzed on a number of levels. Over the years I have developed a number of systems for automatically collecting the data I need for further analysis. The vast majority of new user agents each week are variations on existing user agents in my database, so when the analysis is complete I usually wind up with around two dozen truly new and unique user agents to investigate. I typically do my research on Sunday afternoon and only a few new user agents from major search engines and popular browsers are eventually added to the files in any given week.

User agent detection is still a long ways away from being 100% accurate though because there will always be new user agents that aren't in the files yet and hence won't be properly identified. There are also the bugs in both browscap.dll and PHP's get_browser(), which my files also support, to contend with. And of course there are always spoofed user agents. User agents that claim to be one thing, but in fact are often badly behaved bots masquerading as well behaved bots like Googlebot.

Approximately once a week the raw data in my database is compiled into various reports, including the all-important browscap.ini file for browser identification, and gets uploaded to this website.

You can directly download Browscap files according to your needs from their website.

Configuration (Optional)

First locate your php.ini file in your PHP installation folder, search for browscap and you will find these lines:

; http://php.net/browscap
; browscap = extra/browscap.ini

You can configure this settings, and you can set the location for browscap file.

Setting Up

Put browscap.php (the parser) and php_browscap.ini in the same folder. Next create a new file called test.php in the same directory and put the code shown below inside test.php:


$bc = new Browscap('cache');
$bc->localFile = 'php_browscap.ini';

$data = $bc->getBrowser();

Next create a sub-folder called cache, browscap.php will store browser information for quick access here. Now, visit test.php in your browser (assuming it is on your server), you should see your browser info displayed to the screen!

To access individual parts of browser information simply access $data as an object:

echo $data->Browser;