Php obtains the browser name VERSION instance Program

Source: Internet
Author: User

You can directly use HTTP_USER_AGENT to obtain the browser version in php. However, you need to determine the version of the browser,

Example 1

The Code is as follows: Copy code

Function get_user_browser (){
If (empty ($ _ SERVER ['HTTP _ USER_AGENT ']) {
Return '';
}

$ Agent = $ _ SERVER ['HTTP _ USER_AGENT '];
$ Browser = '';
$ Browser_ver = '';

If (preg_match ('/MSIEs ([^ s |;] +)/I', $ agent, $ regs )){
$ Browser = 'Internet Explorer ';
$ Browser_ver = $ regs [1];
} Elseif (preg_match ('/FireFox/([^ s] +)/I', $ agent, $ regs )){
$ Browser = 'Firefox ';
$ Browser_ver = $ regs [1];
} Elseif (preg_match ('/Maxthon/I', $ agent, $ regs )){
$ Browser = '(Internet Explorer'. $ browser_ver. ') Maxthon ';
$ Browser_ver = '';
} Elseif (preg_match ('/Opera [s |/] ([^ s] +)/I', $ agent, $ regs )){
$ Browser = 'Opera ';
$ Browser_ver = $ regs [1];
} Elseif (preg_match ('/OmniWeb/(v *) ([^ s |;] +)/I', $ agent, $ regs )){
$ Browser = 'omniweb ';
$ Browser_ver = $ regs [2];
} Elseif (preg_match ('/Netscape ([d] *)/([^ s] +)/I', $ agent, $ regs )){
$ Browser = 'netscape ';
$ Browser_ver = $ regs [2];
} Elseif (preg_match ('/safari/([^ s] +)/I', $ agent, $ regs )){
$ Browser = 'safari ';
$ Browser_ver = $ regs [1];
} Elseif (preg_match ('/NetCaptors ([^ s |;] +)/I', $ agent, $ regs )){
$ Browser = '(Internet Explorer'. $ browser_ver. ') NetCaptor ';
$ Browser_ver = $ regs [1];
} Elseif (preg_match ('/Lynx/([^ s] +)/I', $ agent, $ regs )){
$ Browser = 'lynx ';
$ Browser_ver = $ regs [1];
}

If (! Empty ($ browser )){
Return addslashes ($ browser. ''. $ browser_ver );
} Else {
Return 'unknow browser ';
}
}

Instance

The Code is as follows: Copy code

Function get_user_browser ()
{
If (empty ($ _ SERVER ['HTTP _ USER_AGENT '])
{// When the browser does not send the visitor's Information
Return '';
}

$ Agent = $ _ SERVER ['HTTP _ USER_AGENT '];
$ Browser = '';
$ Browser_ver = '';

If (preg_match ('/MSIEs ([^ s |;] +)/I', $ agent, $ regs ))
{
$ Browser = 'Internet Explorer '; // when MSIE is matched, the room that gets the number is in the array $ regs.
$ Browser_ver = $ regs [1];
}
Elseif (preg_match ('/FireFox/([^ s] +)/I', $ agent, $ regs ))
{
$ Browser = 'Firefox '; // when FireFox/is matched, obtain the digit that follows
$ Browser_ver = $ regs [1];
}
Elseif (preg_match ('/Maxthon/I', $ agent, $ regs ))
{
$ Browser = '(Internet Explorer'. $ browser_ver. ') Maxthon ';
$ Browser_ver = '';
}
Elseif (preg_match ('/Opera [s |/] ([^ s] +)/I', $ agent, $ regs ))
{
$ Browser = 'Opera ';
$ Browser_ver = $ regs [1];
}
Elseif (preg_match ('/OmniWeb/(v *) ([^ s |;] +)/I', $ agent, $ regs ))
{
$ Browser = 'omniweb ';
$ Browser_ver = $ regs [2];
}
Elseif (preg_match ('/Netscape ([d] *)/([^ s] +)/I', $ agent, $ regs ))
{
$ Browser = 'netscape ';
$ Browser_ver = $ regs [2];
}
Elseif (preg_match ('/safari/([^ s] +)/I', $ agent, $ regs ))
{
$ Browser = 'safari ';
$ Browser_ver = $ regs [1];
}
Elseif (preg_match ('/NetCaptors ([^ s |;] +)/I', $ agent, $ regs ))
{
$ Browser = '(Internet Explorer'. $ browser_ver. ') NetCaptor ';
$ Browser_ver = $ regs [1];
}
Elseif (preg_match ('/Lynx/([^ s] +)/I', $ agent, $ regs ))
{
$ Browser = 'lynx ';
$ Browser_ver = $ regs [1];
}

If (! Empty ($ browser ))
{
Return addslashes ($ browser. ''. $ browser_ver); // escape quotation marks
}
Else
{
Return 'unknow browser ';
}
}


Determine if it is a spider

Function is_spider ($ record = true) // you can check whether the image is a spider.
{
Static $ spider = NULL;

If ($ spider! = NULL)
{
Return $ spider ;//
}

If (empty ($ _ SERVER ['HTTP _ USER_AGENT '])
{
$ Spider = '';

Return '';
}

$ Searchengine_bot = array (
'Googlebot ',
'Mediapartners-google ',
'Baidider ider + ',
'Msnbot ',
'Yodaobot ',
'Yahoo! Slurp ;',
'Yahoo! Slurp china ;',
'Askspider ',
'Sogou web spider ',
'Sogou push spider'
);

$ Searchengine_name = array (
'Google ',
'Google adsens ',
'Baidu ',
'Msn ',
'Yodao ',
'Yahoo ',
'Yahoo China ',
'Iask ',
'Sogou ',
'Sogou'
);

$ Spider = strtolower ($ _ SERVER ['HTTP _ USER_AGENT ']); // convert the user's browser information to lowercase letters.

Foreach ($ searchengine_bot AS $ key => $ value)
{
If (strpos ($ spider, $ value )! = False) // The value is definitely not equal to the value of 0.
{
$ Spider = $ searchengine_name [$ key]; // return the corresponding seo/seo.html "target =" _ blank "> Search Engine name

Return $ spider;
}
}

$ Spider = '';

Return '';
}

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.