get_browser()
for BrowscapThe 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 Browscap.org.
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.
You just have to call the get_browser_local()
function with:
$user_agent=null
(implied)null
,
then it uses $_SERVER['HTTP_USER_AGENT']
.$return_array=false
(Implied)$db='./browscap.ini'
(Implied, not in the native function)$cache=false
(Implied, not in the native function)return
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).
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) {//https://alexandre.alapetite.fr/doc-alex/php-local-browscap/ //Get browscap.ini on http://browscap.org/ 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; }
To get an idea of the structure of this database:
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
browser_name_regex
This content is protected by a licence
Creative Commons Attribution-ShareAlike 2.0 France “BY-SA (FR)”