View source
<?php
function counter_get_browser() {
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$browser_name = 'Unknown';
$browser_version = '';
$platform = 'Unknown';
$ub = 'Unknown';
if (preg_match('/linux/i', $user_agent)) {
$platform = 'Linux';
}
elseif (preg_match('/macintosh|mac os x/i', $user_agent)) {
$platform = 'Mac';
}
elseif (preg_match('/windows|win32/i', $user_agent)) {
$platform = 'Windows';
}
if (preg_match('/MSIE/i', $user_agent) && !preg_match('/Opera/i', $user_agent)) {
$browser_name = 'Internet Explorer';
$ub = "IE";
}
elseif (preg_match('/Firefox/i', $user_agent)) {
$browser_name = 'Mozilla Firefox';
$ub = "Firefox";
}
elseif (preg_match('/Chrome/i', $user_agent)) {
$browser_name = 'Google Chrome';
$ub = "Chrome";
}
elseif (preg_match('/Safari/i', $user_agent)) {
$browser_name = 'Apple Safari';
$ub = "Safari";
}
elseif (preg_match('/Opera/i', $user_agent)) {
$browser_name = 'Opera';
$ub = "Opera";
}
elseif (preg_match('/Netscape/i', $user_agent)) {
$browser_name = 'Netscape';
$ub = "Netscape";
}
$known = array(
'browser_version',
$ub,
'other',
);
$pattern = '#(?<browser>' . join('|', $known) . ')[/ ]+(?<browser_version>[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern, $user_agent, $matches)) {
}
$i = count($matches['browser']);
if ($i != 1) {
if (strripos($user_agent, "browser_version") < strripos($user_agent, $ub)) {
$browser_version = $matches['browser_version'][0];
}
else {
$browser_version = $matches['browser_version'][1];
}
}
else {
$browser_version = $matches['browser_version'][0];
}
if ($browser_version == null || $browser_version == "") {
$browser_version = "?";
}
return array(
'browser_name' => $browser_name,
'browser_version' => $browser_version,
'platform' => $platform,
'pattern' => $pattern,
);
}