Browser detection these days is a mess! With over 100,000 known browser userAgents we need an easier way of getting info about the user's browser without checking all of the known userAgents manually. That's where browscap.ini and PHP come into play.
What is browscap.ini?
Taken directly from http://browsers.garykeith.com/index.asp: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 awhile. 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.
Basically, browscap.ini was created by microsoft as an easy way to get info about the user's browser. Because microsoft has stopped updating browscap.ini Gary Keith decided to continue updating it for the sake of all man kind. I must say he has done an excellent job of keeping it updated too!
What about PHP?
PHP is an awesome scripting language that has a built in function for using browscap.ini. Unfortunately, that function doesn't always work because most servers either don't have a browscap.ini file, or have an outdated version.The fix
First of all, you have to download the latest browscap.ini from Gary Keith's site. Make sure you get the PHP version!Next, because PHP's built in get_browser() function is unreliable in so many ways, go and download Jonathan Stoppani's browscap.ini parser.
Setting it 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 this inside it:<?php
require('Browscap.php');
$bc = new Browscap('cache');
$bc->localFile = 'php_browscap.ini';
$data = $bc->getBrowser();
print_r($data);
Next create a sub-folder called cache, Browscap.php will store information for quick access here. Now if you 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 tidbits of information simply access $data as an object:
echo $data->Browser;
Drawbacks
Obviously, you have to download the latest browscap.ini file every so often, but that isn't such a big deal now is it?Comment
See what others have to say on this topic, or add your own two cents.