Home  >  Article  >  Backend Development  >  PHP Get Browser Name Version Example Program_PHP Tutorial

PHP Get Browser Name Version Example Program_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 16:57:24833browse

To get the browser version, just use HTTP_USER_AGENT directly in php, but to specifically determine which browser and version we need to determine one by one.

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

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

Example

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 the matching is matched, the part that obtains the number of numbers is in the array in the array

         $browser_ver = $regs[1];
}
elseif (preg_match('/FireFox/([^s]+)/i', $agent, $regs))
{                                                             $browser = 'FireFox'; //When firefox/ is matched, get the number part that follows
         $browser_ver = $regs[1];
}
elseif (preg_match('/Maxthon/i', $agent, $regs))
{
                                                                                                                                                                                                                                                                                                      a            $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                                                                                                                                                                                          $browser_ver = $regs[2];
}
elseif (preg_match('/Netscape([d]*)/([^s]+)/i', $agent, $regs))
{
         $browser                                                                                                                                                                                                                                                                          $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 quotes

}
else
{
          return 'Unknow browser';
}
}


Determine whether it is a spider

function is_spider($record = true) //Determine 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',
        'baiduspider+',
        'msnbot',
        'yodaobot',
        'yahoo! slurp;',
        'yahoo! slurp china;',
        'iaskspider',
        'sogou web spider',
        'sogou push spider'
    );

    $searchengine_name = array(
        'GOOGLE',
        'GOOGLE ADSENSE',
        'BAIDU',
        'MSN',
        'YODAO',
        'YAHOO',
        'Yahoo China',
        'IASK',
        'SOGOU',
        'SOGOU'
    );

    $spider = strtolower($_SERVER['HTTP_USER_AGENT']);    //将用户浏览器信息转成小写

    foreach ($searchengine_bot AS $key => $value)
    {
        if (strpos($spider, $value) !== false)                    //绝对不等于,避免字符串出现为止为0的这种情况
        {
            $spider = $searchengine_name[$key];            //返回对应的seo/seo.html" target="_blank">搜索引擎名

            return $spider;
        }
    }

    $spider = '';

    return '';
}

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/631523.htmlTechArticle获取浏览器版本在php中直接使用HTTP_USER_AGENT就可以了,但是要具体判断是那个浏览器,什么版本我们需要一个个去判断了, 例1 代码如下 复...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn