drt5813 2014-03-24 10:32
浏览 36
已采纳

发现它的浏览器是IE使用HTTP_USER_AGENT

I am trying to write a little script to find out what type of browser the user is using.

I found the following from somewhere (I think it was SO):

   $browser = array(
    'version'   => '0.0.0',
    'majorver'  => 0,
    'minorver'  => 0,
    'build'     => 0,
    'name'      => 'unknown',
    'useragent' => ''
  );

  $browsers = array(
    'firefox', 'msie', 'opera', 'chrome', 'safari', 'mozilla', 'seamonkey', 'konqueror', 'netscape',
    'gecko', 'navigator', 'mosaic', 'lynx', 'amaya', 'omniweb', 'avant', 'camino', 'flock', 'aol'
  );

  if (isset($_SERVER['HTTP_USER_AGENT'])) {
    $browser['useragent'] = $_SERVER['HTTP_USER_AGENT'];
    $user_agent = strtolower($browser['useragent']);
    foreach($browsers as $_browser) {
      if (preg_match("/($_browser)[\/ ]?([0-9.]*)/", $user_agent, $match)) {
        $browser['name'] = $match[1];
        $browser['version'] = $match[2];
        @list($browser['majorver'], $browser['minorver'], $browser['build']) = explode('.', $browser['version']);
        break;
      }
    }
  }

  var_dump($browser);

Out of all the little code snippets I tried, this one worked the best. It correctly identified Chrome and Firefox. However, It can't correctly identify IE, (I am using IE11) because IE doesnt pass any information to the browser. The output from the above code on IE11 is as follows:

array(6) {
  ["version"]=>
  string(3) "5.0"
  ["majorver"]=>
  string(1) "5"
  ["minorver"]=>
  string(1) "0"
  ["build"]=>
  NULL
  ["name"]=>
  string(7) "mozilla"
  ["useragent"]=>
  string(68) "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"
}

Once again, Microsoft have to throw a spanner in the works when it comes to Internet Explorer. What can I do to detect IE? There is no MSIE or even any indication of Internet Explorer in the HTTP_USER_AGENT. How can I detect it?

  • 写回答

1条回答 默认 最新

  • dongzou9047 2014-03-24 12:47
    关注

    Trident is the rendering engine of IE – so whenever that partial string shows up, you can assume that you’re dealing with an Internet Explorer (or someone trying to fake one, of course).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题2
  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备