Copy Code code as follows:
<?php
Define (' IE ', 1);
Define (' FIREFOX ', 2);
Define (' CHROME ', 3);
Define (' OPERA ', 4);
Class Useragentdetect
{
Static $version = Array ();
static $userAgent = ';
static function Getuseragent ()
{
$header = Getallheaders ();
Self:: $userAgent = $header [' user-agent '];
}
function Finduseragentbykey ($ua, $key)
{
$len = strlen ($key);
$start = Strpos ($ua, $key);
$pos = Strpos ($ua, ', $start + $len + 1);
Return substr ($ua, $start + $len, $pos-$len-$start);
}
static function Detectuseragent ()
{
Self:: Getuseragent ();
if (Strpos (self:: $userAgent, ' MSIE ')!= null)
{
Self:: $version [] = IE;
Self:: $version [] =self:: Finduseragentbykey (self:: $userAgent, ' msie ');
}
else if (Strpos (self:: $userAgent, ' gecko/')!= null)
{
Self:: $version [] = FIREFOX;
Self:: $version [] =self:: Finduseragentbykey (self:: $userAgent, ' firefox/');
}
else if (Strpos (self:: $userAgent, ' applewebkit/')!= null)
{
Self:: $version [] = CHROME;
Self:: $version [] = self:: Finduseragentbykey (self:: $userAgent, ' chrome/');
}
else if (Strpos (self:: $userAgent, ' presto/')!= null)
{
Self:: $version [] = OPERA;
Self:: $version [] = self:: Finduseragentbykey (self:: $userAgent, ' opera/');
}
Else
{
}
Return self:: $version;
}
function Matchuseragent ($ua, $key)
{
Preg_match ("/$key. ( [^\s]*?] \s/", $ua, $out);
return $out [1];
}
}
$ua =useragentdetect::d etectuseragent ();
Print_r ($ua);