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, but you need to determine which version is the browser. the instance code is as follows: functionget_user_ B... you can directly use HTTP_USER_AGENT to obtain the browser version in php. However, you need to determine the version of the browser. the instance 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('/MSIE\s([^\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('/Chrome\/([\d.]+)/i', $agent, $regs)) {        $browser = 'Chrome';        $browser_ver = $regs[1];    } 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';    }}

Note: chrome uses some webkit code, which is developed by Apple. Therefore, chrome recognition must be placed before safari.

Determine if it is a spider:

Function is_spider ($ record = true) // judge whether it 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', 'baidu', 'msnbot ', 'yodaobot', 'Yahoo! Slurp; ', 'Yahoo! Slurp china; ', 'iaskspider', 'sogou web spider ', 'sogou push spider' 'haosou'); $ searchengine_name = array ('Google ', 'Google adsen ', 'baidu', 'MSN ', 'Yoda', 'Yahoo', 'Yahoo China', 'iask ', 'sogou', 'sogou' haosou '); $ spider = strtolower ($ _ SERVER ['http _ USER_AGENT ']); // Convert the user's browser information to lowercase foreach ($ searchengine_bot AS $ key => $ value) {if (strpos ($ spider, $ value )! = False) // It is definitely not equal to the value. avoid the case where the string is 0 until it appears {$ spider = $ searchengine_name [$ key]; // return the corresponding seo/seo.html "target =" _ blank "> Search Engine Name return $ spider ;}$ spider =''; return '';}


Article address:

Reprint at will ^ please include the address of this article!

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.