Browscap.php – how to use it!

Browscap.php is a piece of code that allow you to use the famous formerly Gary Keith’s browser information database [UPDATED] which now it’s keep by R. Moose and hosted at new browscap.ini download page. This piece of code allows PHP 5 coders to use the php_browscap.ini without the needs to configure the php.ini. As many of you probably know, not all hosting services allow you access to the php.ini where you configure the location of the browscap file. So when you try to use the function get_browser(), to detect the browser the client-side is using and adjust or predetermining a certain behavior for your web page, you found yourself stuck.

Now with the Browscap.php you do not longer need access to the php.ini file, this code not only let you use the browscap.ini but also updates it automatically. But for this you need to tweak the code a little bit. (This if you have an error when using the code).

Today I was working with a customer web page and find a PHP-Error saying there was a conflict in the Browscap.php line 308. If you have that error don’t panic. There are two versions of the browscap file, the regular browscap.ini to be use with ASP server side scripting and the php_browscap.ini. In the documentation for the Browscap.php says that the code can read either, but in my experience is not entirely truth, sometimes works and sometimes not, don’t ask me why.

You have to download the php_browscap.ini and remove the browscap.ini from the location I will specify.

But let’s start configuring the Browscap.php…

First of all you need to include your Browscap.php to the header of your page. If your using systems like Worpress or Drupal you might want to put them in your header.php file or however you call it. As is in the Browscap.php quick start documentation:

// Loads the class
require 'path/to/Browscap.php';

// Creates a new Browscap object (loads or creates the cache)
$bc = new Browscap(‘path/to/the/cache/dir’);

// Gets information about the current browser’s user agent
$current_browser = $bc->getBrowser();

// Output the result
echo ‘<pre>’; // some formatting issues ūüėČ
print_r($current_browser);
echo ‘</pre>’;

We are going to obviate the last section under the comment ‘Output the result’ so your code should look like this…

// Loads the class
require 'path/to/Browscap.php';

// Creates a new Browscap object (loads or creates the cache)
$bc = new Browscap(‘path/to/the/cache/dir’);

// Gets information about the current browser’s user agent
$current_browser = $bc->getBrowser();

Let say that my Browscap.php is in the root of my site the first lines of code should read:


// Loads the class
require '/Browscap.php';

or you can use:


// Loads the class
require ($_SERVER('DOCUMENT_ROOT').'/Browscap.php');

either way will work just fine. Now the next lines of code should point out the location where you php_browscap.ini it is. For example I will pretend that my php_browscap.ini is in a folder call ‘browscap’, the lines of code should read:


// Creates a new Browscap object (loads or creates the cache)
$bc = new Browscap('/browscap/');

As you can see I didn’t point it out directly to the file but just the location where the file is. The next line of code will set the result in a variable call $current_browser but you can call it whatever you want, in my case I prefer $curbrows is shorter and still I understand what it is…


// Gets information about the current browser's user agent
$current_browser = $bc->getBrowser();

This should be enough to have everything ready to start coding your script. Just remember that contrary to the traditional get_browser() function, this is not actually a regular Array, this is more like an object so you can not use $current_browser[‘browser’] as before, you should use $current_browser->Browser for example:

if($current_browser->Browser == 'IE'){echo '<style>p {margin:0px;}</style>';}

Other than that you can write your script as always.

The Update Problem

If your having problems with the Browscap try the following tips… If you don’t have any problem don’t mess with it. I did those changes because when I installed the new Apache Server for my local server I get some errors and this help me fixed them. The error code was a PHP error pointing to line 308 of the Browscap.php, if you’re having this error try this. It works for me as in Apache as with IIS7 with FastCGI.

Now with the update problem. Because it is documented that you can use either browscap files (browscap.ini and php_browscap.ini) the code will look for an update of the browscap.ini, to make the tweak to update the php version of browscap you need to find the following line of code (line 92 at the moment I wrote this blog):

public $remoteIniUrl    = 'http://browsers.garykeith.com/stream.asp?BrowsCapINI';

and modify it to:

public $remoteIniUrl    = 'http://browsers.garykeith.com/stream.asp?PHP_BrowsCapINI';

UPDATE [02/15/2014] – Thanks to James Titcumb for the comment.
The new permanent location to download the browscap is @ http://browscap.org/stream?q=BrowsCapINI or http://browscap.org/stream?q=PHP_BrowsCapINI

That points out to the location for the download of the php_browscap.ini file. I discover this as I said today, because every time I try to delete the browscap.ini file appeared again… ūüôā imagine my surprise. Now you don’t have to worry about having another line 308 error in you Browscap.php.

Also look for the line:
public $iniFilename     = 'browscap.ini';

and modify it to:
public $iniFilename     = 'php_browscap.ini';

Another note:

I tried to use the Browscap.php from http://github.com/garetjax/phpbrowscap as instructed by the Browscap.php project home page and I’ve always get an error from that file. At the moment I forgot the error code but I will try to get it again. To be sure download the one in http://code.google.com/p/phpbrowscap/ if your using the code for the first time. Just in case.

[UPDATE] 03/13/2013 – Browscap.php is hosted in github, so the best bet is to download it from there. You might want also to check every call outside your local url, any file_get_content or any other cross browser request and make sure to add a try/catch option to avoid returning a Notice or Fatal Error from PHP.

Happy Coding

This entry was posted in Tutorials and tagged , , , , , , by Rafael Vila. Bookmark the permalink.

About Rafael Vila

I'm a Son of God, Father of three extremely awesome sons, husband of my sweetie pie Estella. I work as a Graphic Designer and a Web Developer in a Texan Company called Stealth Products, Inc. I've been blessed with a beautiful family and a great community of Faith. I believe God is the only God, who sent his son Jesus who become our Saviour and Redeemer Christ. The mercy and compassion of God is my daily wonder.

3 thoughts on “Browscap.php – how to use it!

  1. obviously the whole project is corrupted like most of the stuff from github…
    I get only errors like “Fatal error: Class ‘Browscap’ not found in /home/www/web648/html/browscaptest.php on line 6”
    With code like this you better keep your fingers off!

Comments are closed.