27 August 2008

Get visistor's OS and Browser with PHP - Script 1


/**************************************************
* User Agent Detector
* -------------------
*
* Deluxe Portal Version 2.0
*
* This SINGLE FILE is Licensed under the LGPL which
* can be found at
* [url=http://www.gnu.org/licenses/lgpl.txt]http://www.gnu.org/licenses/lgpl.txt[/url]
**************************************************/
$_REMOTE = array();
$_REMOTE['USER_AGENT'] = $_SERVER['HTTP_USER_AGENT'];
$_REMOTE['BROWSER']['TYPE'] = 0;
$_REMOTE['BROWSER']['VERSION'] = 0;
$_REMOTE['BROWSER']['GECKO'] = 0;
$_REMOTE['OS']['PLATFORM'] = 0;
$_REMOTE['OS']['TYPE'] = 0;
$_REMOTE['OS']['VERSION'] = 0;

if (strpos($_REMOTE['USER_AGENT'], 'Opera') === 0)
{
$_REMOTE['BROWSER']['TYPE'] = 'Opera';
list($version, $dummy) = explode(' ', $_REMOTE['USER_AGENT'], 2);
list($dummy, $version) = explode('/', $version, 2);
if (strpos($version, 'v') === 0) $version = dp_substr($version, 1);
}
elseif (strpos($_REMOTE['USER_AGENT'], 'Mozilla') === 0)
{
if (strpos($_REMOTE['USER_AGENT'], 'Opera'))
{
$_REMOTE['BROWSER']['TYPE'] = 'Opera';
list($dummy, $version) = explode('\)', $_REMOTE['USER_AGENT'], 2);
list($dummy, $dummy, $version) = explode(' ', $version);
if (strpos($version, 'v') === 0) $version = dp_substr($version, 1);
}
elseif (strpos($_REMOTE['USER_AGENT'], '(compatible'))
{
if (strpos($_REMOTE['USER_AGENT'], 'Konqueror'))
{
$_REMOTE['BROWSER']['TYPE'] = 'Konqueror';
list($dummy, $version) = explode('Konqueror/', $_REMOTE['USER_AGENT'], 2);
list($version, $dummy) = explode(';', $version, 2);
}
elseif (strpos($_REMOTE['USER_AGENT'], 'MSIE'))
{
$_REMOTE['BROWSER']['TYPE'] = 'Internet Explorer';
list($dummy, $version) = explode('MSIE', $_REMOTE['USER_AGENT'], 2);
list($version, $dummy) = explode(';', $version, 2);
}
}
else
{
if (strpos($_REMOTE['USER_AGENT'], 'Netscape6'))
{
$_REMOTE['BROWSER']['TYPE'] = 'Netscape';
list($dummy, $version) = explode('Netscape6/', $_REMOTE['USER_AGENT'], 2);
}
elseif (strpos($_REMOTE['USER_AGENT'], 'Gecko'))
{
$_REMOTE['BROWSER']['TYPE'] = 'Mozilla';
list($dummy, $version) = explode('rv:', $_REMOTE['USER_AGENT'], 2);
list($version, $dummy) = explode('\)', $version, 2);
}
elseif (strpos($_REMOTE['USER_AGENT'], 'Sun'))
{
$_REMOTE['BROWSER']['TYPE'] = 'Hot Java';
list($version, $dummy) = explode(' ', $_REMOTE['USER_AGENT'], 2);
list($dummy, $version) = explode('/', $version, 2);
}
else
{
$_REMOTE['BROWSER']['TYPE'] = 'Netscape';
list($version, $dummy) = explode(' ', $_REMOTE['USER_AGENT'], 2);
list($dummy, $version) = explode('/', $version, 2);
}
}
}
elseif (strpos($_REMOTE['USER_AGENT'], 'amaya') === 0)
{
$_REMOTE['BROWSER']['TYPE'] = 'Amaya';
list($dummy, $version) = explode('amaya/', $_REMOTE['USER_AGENT'], 2);
list($version, $dummy) = explode(' ', $version, 2);
}
elseif (strpos($_REMOTE['USER_AGENT'], 'EPOC') === 0)
{
$_REMOTE['BROWSER']['TYPE'] = 'EPOC Web';
list($dummy, $version) = explode('WTL/', $_REMOTE['USER_AGENT'], 2);
list($version, $dummy) = explode(' ', $version, 2);
}
if (isset($version)) $_REMOTE['BROWSER']['VERSION'] = trim($version);
if (preg_match('/gecko\/([0-9]+)/i', $_REMOTE['USER_AGENT'], $match)) $_REMOTE['BROWSER']['GECKO'] = $match[1];
$_REMOTE['OS']['PLATFORM'] = '*NIX';
if (preg_match_all('/(win[dows]*)[\s]?([0-9a-z]*)[\w\s]?([a-z0-9.]*)/i', $_REMOTE['USER_AGENT'], $match))
{
$v = $match[2][count($match[0])-1];
$v2 = $match[3][count($match[0])-1];
$_REMOTE['OS']['PLATFORM'] = 'Win32';
$_REMOTE['OS']['VERSION'] = $v2;
if (stristr($v,'NT'))
{
if ($v2 == '5.0')
$v = 'Windows 2000';
elseif ($v2 == '5.1')
$v = 'Windows XP';
elseif ($v2 == '5.2')
$v = 'Windows 2003';
elseif ($v2 == '6.0')
$v = 'Windows Codename Longhorn';
else
$v = 'Windows NT';
}
elseif (stristr($v,'9x'))
$v = 'Windows 9x';
elseif ($v.$v2 == '16bit')

{
$v = 'Windows';
$_REMOTE['OS']['PLATFORM'] = 'Win16';
}
else
$v .= $v2;

if (empty($v)) $v = 'win';
$_REMOTE['OS']['TYPE'] = $v;
}
elseif( preg_match('/os\/2|ibm-webexplorer/i', $_REMOTE['USER_AGENT']))
{
$_REMOTE['OS']['TYPE'] = 'OS/2';
$_REMOTE['OS']['PLATFORM'] = 'OS/2';
}
elseif (preg_match('/(68)[k0]{1,3}|[p\S]{1,5}(pc)/i', $_REMOTE['USER_AGENT'], $match))
{
$_REMOTE['OS']['TYPE'] = 'Mac OS';
$platform = !empty($match[1]) ? '68k' : '';
$platform = !empty($match[2]) ? 'Power PC' : $platform;
$_REMOTE['OS']['PLATFORM'] = $platform;
}
elseif (preg_match('/(sun|i86)[os\s]*([0-9]*)/i',$_REMOTE['USER_AGENT'],$match))
{
if(!stristr('sun',$match[1]))
{
$match[1] = 'sun'.$match[1];
$_REMOTE['OS']['TYPE'] = $match[1];
$_REMOTE['OS']['VERSION'] = $match[2];
}
elseif (preg_match('/(irix)[\s]*([0-9]*)/i', $_REMOTE['USER_AGENT'], $match))
{
$_REMOTE['OS']['TYPE'] = $match[1];
$_REMOTE['OS']['VERSION'] = $match[2];
}
elseif (preg_match('/(hp-ux)[\s]*([0-9]*)/i', $_REMOTE['USER_AGENT'], $match))
{
$_REMOTE['OS']['TYPE'] = 'HP-UNIX';
$_REMOTE['OS']['VERSION'] = (int) $match[2];
}
elseif (preg_match('/aix([0-9]*)/i', $_REMOTE['USER_AGENT'], $match))
{
$_REMOTE['OS']['TYPE'] = 'AIX';
$_REMOTE['OS']['VERSION'] = $match[1];
}
elseif (preg_match('/dec|osfl|alphaserver|ultrix|alphastation/i', $_REMOTE['USER_AGENT'], $match))
$_REMOTE['OS']['TYPE'] = 'DEC';
elseif (preg_match('/vax|openvms/i', $_REMOTE['USER_AGENT'], $match))
$_REMOTE['OS']['TYPE'] = 'VMS';
elseif (preg_match('/sco|unix_sv/i', $_REMOTE['USER_AGENT'], $match))
$_REMOTE['OS']['TYPE'] = 'SCO';
elseif (stristr('unix_system_v', $_REMOTE['USER_AGENT']))
$_REMOTE['OS']['TYPE'] = 'UNIXWARE';
elseif (stristr('ncr', $_REMOTE['USER_AGENT']))
$_REMOTE['OS']['TYPE'] = 'MPRAS';
elseif (stristr('reliantunix', $_REMOTE['USER_AGENT']))
$_REMOTE['OS']['TYPE'] = 'Reliant';
elseif(stristr('sinix', $_REMOTE['USER_AGENT']))
$_REMOTE['OS']['TYPE'] = 'SINIX';
elseif (preg_match('/(free)?(bsd)/i', $_REMOTE['USER_AGENT'], $match))
$_REMOTE['OS']['TYPE'] = $match[1].$match[2];
elseif(preg_match('/x11|inux/i', $_REMOTE['USER_AGENT'], $match))
$_REMOTE['OS']['TYPE'] = 'Linux';
elseif (stristr('epoc', $_REMOTE['USER_AGENT']))
{
$_REMOTE['OS']['TYPE'] = 'EPOC';
$_REMOTE['OS']['PLATFORM'] = 'EPOC';
}
else
$_REMOTE['OS']['PLATFORM'] = 'UNKNOWN';
}

print_r($_REMOTE);

No comments: