php

php browser detection class

<?php
class Browser {
/**
Figure out what browser is used, its version and the platform it is
running on.

The following code was ported in part from JQuery v1.3.1
*/
public static function detect() {
$userAgent = strtolower($_SERVER[‘HTTP_USER_AGENT’]);

// Identify the browser. Check Opera and Safari first in case of spoof. Let Google Chrome be identified as Safari.
if (preg_match(‘/opera/’, $userAgent)) {
$name = ‘opera’;
}
elseif (preg_match(‘/webkit/’, $userAgent)) {
$name = ‘safari’;
}
elseif (preg_match(‘/msie/’, $userAgent)) {
$name = ‘msie’;
}
elseif (preg_match(‘/mozilla/’, $userAgent) && !preg_match(‘/compatible/’, $userAgent)) {
$name = ‘mozilla’;
}
else {
$name = ‘unrecognized’;
}

// What version?
if (preg_match(‘/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/’, $userAgent, $matches)) {
$version = $matches[1];
}
else {
$version = ‘unknown’;
}

// Running on what platform?
if (preg_match(‘/linux/’, $userAgent)) {
$platform = ‘linux’;
}
elseif (preg_match(‘/macintosh|mac os x/’, $userAgent)) {
$platform = ‘mac’;
}
elseif (preg_match(‘/windows|win32/’, $userAgent)) {
$platform = ‘windows’;
}
else {
$platform = ‘unrecognized’;
}

return array(
‘name’ => $name,
‘version’ => $version,
‘platform’ => $platform,
‘userAgent’ => $userAgent
);
}
}
$browser = Browser::detect();

?>

Advertisements