【问题标题】:HTTP_USER_AGENT in PHP says Mozilla for IEPHP 中的 HTTP_USER_AGENT 表示 Mozilla for IE
【发布时间】:2017-02-03 15:26:42
【问题描述】:

我需要能够在 PHP 脚本中指定 IE 11 和 Firefox。我有以下功能。但是,在 IE 中它返回 Mozilla。有没有其他方法可以区分 Firefox 和 IE?

function browser() {
 $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
 // you can add different browsers with the same way ..
 if(preg_match('/(chromium)[ \/]([\w.]+)/', $ua))
         $browser = 'chromium';
 elseif(preg_match('/(chrome)[ \/]([\w.]+)/', $ua))
         $browser = 'chrome';
 elseif(preg_match('/(safari)[ \/]([\w.]+)/', $ua))
         $browser = 'safari';
 elseif(preg_match('/(opera)[ \/]([\w.]+)/', $ua))
         $browser = 'opera';
 elseif(preg_match('/(msie)[ \/]([\w.]+)/', $ua))
         $browser = 'msie';
 elseif(preg_match('/(mozilla)[ \/]([\w.]+)/', $ua))
         $browser = 'mozilla';

 preg_match('/('.$browser.')[ \/]([\w]+)/', $ua, $version);
 return array($browser,$version[2],'name'=>$browser,'version'=>$version[2]);
}

谢谢!

【问题讨论】:

标签: php internet-explorer firefox browser


【解决方案1】:

Internet Explorer 11 不包含“MSIE”字符串,因为他们不希望人们认为它与旧版本的 IE 存在相同的问题。

您可以添加如下行:

elseif(preg_match('/(trident)[ \/]([\w.]+)/', $ua))
    $browser = 'msie';
elseif(preg_match('/(firefox)[ \/]([\w.]+)/', $ua))
    $browser = 'firefox';

每个网络浏览器都会在其用户代理字符串中包含单词“Mozilla”(它可以追溯到 Netscape 时代。)

【讨论】:

    猜你喜欢
    • 2016-01-28
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    • 2011-02-04
    • 2011-10-15
    • 1970-01-01
    相关资源
    最近更新 更多