Php get the browser name version instance project_php tutorial

Source: Internet
Author: User
Php obtains the browser name version instance program. 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 below is used to obtain the browser version. you can use HTTP_USER_AGENT directly in php, but you need to determine which version is the browser,

Example 1

The code is as follows:

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:

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 '';
}

Example 1 code is as follows...

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.