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).

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

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题