By Alexandre Alapetite on 2004-05-29; updated 2010-02-06

PHP standalone get_browser() for Browscap

The browscap.ini database contains the signatures of the various Web browsers and other clients, and links them to a list of characteristics, like their exact name, and some capacities like handling frames, javascript, etc.

Demonstration of Browscap from Gary Keith.

PHP has a native function get_browser(), which is able to read this browscap.ini database. But since this requires maintaining the database up to date, which is done by third parties, many hosting configurations have not enabled this function.

I propose on this page a get_browser_local() function, written in PHP, which mimics the native get_browser() function. My function can be used on any PHP hosting configuration, and it only requires putting the browscap.ini file in a directory where it can be read.

Download the module

français

Table of contents

Exit

Utilisation

Installation

  1. Download the php-local-browscap module (1.5) and rename it php-local-browscap.php   (2010-02-06)
  2. Download php_browscap.ini from Gary Keith and rename it browscap.ini

How to use this library

You just have to call the get_browser_local() function with:

$user_agent=null (implied)
The signature of the browser to be analysed. If this parameter is left to null, then it uses $_SERVER['HTTP_USER_AGENT'].
$return_array=false (Implied)
When this parameter is activated, the function returns an array instead of an object.
$db='./browscap.ini' (Implied, not in the native function)
Allows specifying the path of the browscap.ini database, otherwise assumes that it is in the current directory.
$cache=false (Implied, not in the native function)
Specify if the database can be kept in memory, to improve performances when querying this function more than once.
return
Returns an object (or an array, if asked to do so in the second parameter) with the capacities of the browser.

Example

Basic utilisation:

example.php

<?php
if (get_cfg_var('browscap'))
 $browser=get_browser(); //If available, use PHP native function
else
{
 require_once('php-local-browscap.php');
 $browser=get_browser_local();
}
print_r($browser);
?>

To get instructions on how to activate the native get_browser() function of PHP, see my documentation about setting up PHP with Apache2 (in French).


Source code

php-local-browscap.php

<?php
$browscapIni=null; //Cache
$browscapPath=''; //Cached database

function _sortBrowscap($a,$b)
{
 $sa=strlen($a);
 $sb=strlen($b);
 if ($sa>$sb) return -1;
 elseif ($sa<$sb) return 1;
 else return strcasecmp($a,$b);
}

function _lowerBrowscap($r) {return array_change_key_case($r,CASE_LOWER);}

function get_browser_local($user_agent=null,$return_array=false,$db='./browscap.ini',$cache=false)
{//http://alexandre.alapetite.fr/doc-alex/php-local-browscap/
 //Get php_browscap.ini on http://browsers.garykeith.com/downloads.asp
 if (($user_agent==null)&&isset($_SERVER['HTTP_USER_AGENT'])) $user_agent=$_SERVER['HTTP_USER_AGENT'];
 global $browscapIni;
 global $browscapPath;
 if ((!isset($browscapIni))||(!$cache)||($browscapPath!==$db))
 {
  $browscapIni=defined('INI_SCANNER_RAW') ? parse_ini_file($db,true,INI_SCANNER_RAW) : parse_ini_file($db,true);
  $browscapPath=$db;
  uksort($browscapIni,'_sortBrowscap');
  $browscapIni=array_map('_lowerBrowscap',$browscapIni);
 }
 $cap=null;
 foreach ($browscapIni as $key=>$value)
 {
  if (($key!='*')&&(!array_key_exists('parent',$value))) continue;
  $keyEreg='^'.str_replace(
   array('\\','.','?','*','^','$','[',']','|','(',')','+','{','}','%'),
   array('\\\\','\\.','.','.*','\\^','\\$','\\[','\\]','\\|','\\(','\\)','\\+','\\{','\\}','\\%'),
   $key).'$';
  if (preg_match('%'.$keyEreg.'%i',$user_agent))
  {
   $cap=array('browser_name_regex'=>strtolower($keyEreg),'browser_name_pattern'=>$key)+$value;
   $maxDeep=8;
   while (array_key_exists('parent',$value)&&array_key_exists($parent=$value['parent'],$browscapIni)&&(--$maxDeep>0))
    $cap+=($value=$browscapIni[$parent]);
   break;
  }
 }
 if (!$cache) $browscapIni=null;
 return $return_array ? $cap : (object)$cap;
}

Extract of browscap.ini

To get an idea of the structure of this database:

php_browscap.ini

[Mozilla 1.7]
browser="Mozilla"
version=1.7
majorver=1
minorver=7
css=2
frames=True
iframes=True
tables=True
cookies=True
backgroundsounds=False
vbscript=False
javascript=True
javaapplets=True
activexcontrols=False
cdf=False
aol=False
beta=False
win16=False
crawler=False
stripper=False
wap=False
netclr=False

[Mozilla/5.0 (Windows; ?; Windows NT 5.1; *; rv:1.7*) Gecko/*]
parent=Mozilla 1.7
platform=WinXP

History

1.5 2010-02-06
Compatibility with PHP 5.3
1.4 2008-01-17
Tolerance of some errors in the database
1.3 2006-09-09
Update for the new php_browscap.ini format
Convert attributes to lower case
Returns the longest match rather than the first one
1.2.2 2006-08-15
Links updated for http://browsers.garykeith.com
1.2.1 2005-11-25
Corrected a bug when the browser is not found in the database
1.2 2005-11-24
Added a cache system
Corrected a bug in the first parameter
1.1 2005-06-06
Details in the display of browser_name_regex
1.0 2005-05-29
Initial distribution

Licence

This content is protected by a licence Creative Commons Attribution-ShareAlike 2.0 France “BY-SA (FR)” [Creative Commons License]


Comments

object : View comments

http://alexandre.alapetite.fr

Back