Thursday, September 28, 2006

Browser detection in PHP

Did you know that PHP has a built in function to detect which browser your visitor is running?
There is PHP’s get_browser function.

Here’s how to use this function:

< ? $visitorbrowser = get_browser(null,true); print_r($visitorbrowser); ?>

The output of this function is an object which contains details about the user’s browser and looks something like the one below:

[browser_name_regex] => ^mozilla/5.0 (windows; .*; windows nt 5.1.*) gecko/.* firefox/1.5.*$
[browser_name_pattern] => Mozilla/5.0 (Windows; *; Windows NT 5.1*) Gecko/* Firefox/1.5*
[parent] => Firefox 1.5
[platform] => WinXP
[browser] => Firefox
[version] => 1.5
[majorver] => 1
[minorver] => 5
[css] => 2
[frames] => 1
[iframes] => 1
[tables] => 1
[cookies] => 1
[backgroundsounds] =>
[vbscript] =>
[javascript] => 1
[javaapplets] => 1
[activexcontrols] =>
[cdf] =>
[aol] =>
[beta] =>
[win16] =>
[crawler] =>
[stripper] =>
[wap] =>
[ismobiledevice] =>
[netclr] =>

To get PHP to detect your browser properly, you’ll have to get the updated browsecap.ini file and set the path to the file in your php.ini file.

To set the path to browsecap.ini file, add the following entries in your php.ini file:

[browscap]
browscap = /path/to/browscap.ini

Links:

browsecap.ini file: http://browsers.garykeith.com/downloads.asp
PHP get_browser function lookup: http://in.php.net/manual/en/function.get-browser.php

No comments: