Code: Select all
<?php
function feyd_get_browser($user_agent = null, $return_array = false) {
$ini = 'php_browscap.ini';
$default = 'Unknown';
if ($user_agent === null)
{
if(isset($_SERVER['HTTP_USER_AGENT']))
{
$user_agent = $_SERVER['HTTP_USER_AGENT'];
}
else
{
$user_agent = $default;
}
}
$data = parse_ini_file($ini,true);
$final = array();
$found = false;
foreach($data as $pattern => $info)
{
$name_regex = '^' . str_replace(array('?','*'),array('.','.*'),strtolower($pattern)) . '$';
$regex = '#^' . str_replace(array('\\?','\\*','#'),array('.','.*?','\\#'),preg_quote($pattern)) . '$#i';
if (preg_match($regex,$user_agent))
{
$found = true;
$final['browser_name_pattern'] = $pattern;
$final['browser_name_regex'] = $name_regex;
if (isset($info['parent']) and isset($data[$info['parent']]))
{
$final = array_merge($final,$data[$info['parent']],$info);
}
else
{
$final = array_merge($final,$info);
}
break;
}
}
if(!$found) {
$final = $data[$default];
}
if ($return_array)
{
return $final;
}
else
{
return (object)$final;
}
}
echo '<pre>';
var_export(feyd_get_browser());
echo "\n";
var_export(feyd_get_browser(''));
echo "\n";
var_export(feyd_get_browser(null,true));
echo "\n";
var_export(feyd_get_browser('',true));
echo '</pre>';
?>
Code: Select all
stdClass::__set_state(array(
'browser_name_pattern' => 'Mozilla/5.0 (Windows; *; Windows NT 5.1; *rv:*) Gecko/* Firefox/1.5*',
'browser_name_regex' => '^mozilla/5.0 (windows; .*; windows nt 5.1; .*rv:.*) gecko/.* firefox/1.5.*$',
'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' => '',
'netclr' => '',
'parent' => 'Firefox 1.5',
'platform' => 'WinXP',
))
stdClass::__set_state(array(
'browser_name_pattern' => '*',
'browser_name_regex' => '^.*$',
'browser' => 'Default Browser',
'css' => '0',
'frames' => '',
'iframes' => '',
'tables' => '1',
'cookies' => '',
'backgroundsounds' => '',
'vbscript' => '',
'javascript' => '',
'javaapplets' => '',
'activexcontrols' => '',
'cdf' => '',
'aol' => '',
'beta' => '',
'Win16' => '',
'crawler' => '',
'stripper' => '',
'wap' => '',
'netclr' => '',
))
array (
'browser_name_pattern' => 'Mozilla/5.0 (Windows; *; Windows NT 5.1; *rv:*) Gecko/* Firefox/1.5*',
'browser_name_regex' => '^mozilla/5.0 (windows; .*; windows nt 5.1; .*rv:.*) gecko/.* firefox/1.5.*$',
'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' => '',
'netclr' => '',
'parent' => 'Firefox 1.5',
'platform' => 'WinXP',
)
array (
'browser_name_pattern' => '*',
'browser_name_regex' => '^.*$',
'browser' => 'Default Browser',
'css' => '0',
'frames' => '',
'iframes' => '',
'tables' => '1',
'cookies' => '',
'backgroundsounds' => '',
'vbscript' => '',
'javascript' => '',
'javaapplets' => '',
'activexcontrols' => '',
'cdf' => '',
'aol' => '',
'beta' => '',
'Win16' => '',
'crawler' => '',
'stripper' => '',
'wap' => '',
'netclr' => '',
)
I just wrote this from scratch. The tested output is from PHP 5.1.2