Copy codeThe Code is as follows:
<? Php
Define ('ie', 1 );
Define ('Firefox ', 2 );
Define ('chromi', 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, 'msi ')! = Null)
{
Self: $ version [] = IE;
Self: $ version [] = self: findUserAgentByKey (self: $ userAgent, 'msi ');
}
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: detectUserAgent ();
Print_r ($ ua );