PHP PHP浏览器检测指示灯

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP PHP浏览器检测指示灯相关的知识,希望对你有一定的参考价值。

<?php

function browser_info($agent=null) {
  // Declare known browsers to look for
  $known = array('msie', 'firefox', 'safari', 'webkit', 'opera', 'netscape',
    'konqueror', 'gecko');

  // Clean up agent and build regex that matches phrases for known browsers
  // (e.g. "Firefox/2.0" or "MSIE 6.0" (This only matches the major and minor
  // version numbers.  E.g. "2.0.0.6" is parsed as simply "2.0"
  $agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
  $pattern = '#(?<browser>' . join('|', $known) .
    ')[/ ]+(?<version>[0-9]+(?:\.[0-9]+)?)#';

  // Find all phrases (or return empty array if none found)
  if (!preg_match_all($pattern, $agent, $matches)) return array();

  // Since some UAs have more than one phrase (e.g Firefox has a Gecko phrase,
  // Opera 7,8 have a MSIE phrase), use the last one found (the right-most one
  // in the UA).  That's usually the most correct.
  $i = count($matches['browser'])-1;  
  return array($matches['browser'][$i] => $matches['version'][$i],
  				'browser' => $matches['browser'][$i],
  				'version' => $matches['version'][$i]);
}
//get browser info
$ua = browser_info();

//show what's returned
echo '<pre>';
echo print_r( $ua );
echo '</pre>';

/*
// Various browser tests you can do with the returned array ...
if ($ua['firefox']) ... // true
if ($ua['firefox'] > 3) ... // true
if ($ua['firefox'] > 4) ... // false
if ($ua['browser'] == 'firefox') ... // true
if ($ua['version'] > 3.5) ... // true
if ($ua['msie']) ... // false ('msie' key not defined)
if ($ua['opera'] > 3) ... // false ('opera' key not defined)
if ($ua['safari'] < 3) ... // false also ('safari' key not defined)
*/

以上是关于PHP PHP浏览器检测指示灯的主要内容,如果未能解决你的问题,请参考以下文章

PHP Php浏览器检测类

php 使用PHP自动检测浏览器语言

检测 PHP 中的浏览器语言

PHP 浏览器检测和重定向

用 php 检测浏览器的最佳方法是啥?

在 PHP 中检测浏览器退出