Php testing useragent version example _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags php define
Sample useragent version detected by php. Copy the code as follows :? Phpdefine (IE, 1); define (FIREFOX, 2); define (CHROME, 3); define (OPERA, 4); classUserAgentDetect {static $ versionarray (); static $ userAgent The code is as follows:


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 );

The http://www.bkjia.com/PHPjc/745210.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/745210.htmlTechArticle code is as follows :? Php define ('ie', 1); define ('Firefox ', 2); define ('Chrome', 3); define ('Opera ', 4 ); class UserAgentDetect {static $ version = array (); static $ userAgent...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.