【问题标题】:why does the $_SERVER['HTTP_USER_AGENT'] not return MSIE [duplicate]为什么 $_SERVER['HTTP_USER_AGENT'] 不返回 MSIE [重复]
【发布时间】:2014-11-30 23:03:31
【问题描述】:

我正在尝试做一些 php 浏览器测试。当我看着

$_SERVER['HTTP_USER_AGENT'

我发现它返回了这个:

Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko

即使我使用的是 IE 11。

当我在 Chrome 上时,它返回了这个:

Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36

这更有意义。为什么 IE 中没有 MSIE,我该如何定位它?

【问题讨论】:

标签: php browser


【解决方案1】:

"Trident" 是 MSIE 11 的布局引擎。如您所见,当您在 IE 11 上时,它加载为 Trident/7.0; rv:11.0),就像您在 Chrome 上时一样,它加载为 AppleWebKit/537.36

如果您想获取有关浏览器的更多信息,您可以随时使用 PHP 的 get_browser() 函数。

【讨论】:

  • 从技术上讲,它是来自 MSIE8 的布局引擎
  • @oPi,你是对的。但是,它仍被列为 IE11 的一部分,在 Wikipedia 页面的 "Versions" 部分中。
  • 大声笑,这是我的错。我读到“是 MSIE 11 的布局引擎”。对不起
【解决方案2】:

这是因为 8 的 MSIE 通过 TRIDENT 标记其版本。我很久以前就找到了这个脚本。它还检测浏览器是否处于兼容模式。它可能会对你有所帮助,但它在 JS 中:

编辑:通过简单的搜索,我在github找到了原始代码。

var ieUserAgent = {
    init: function () {
        // Get the user agent string
        var ua = navigator.userAgent;
        this.compatibilityMode = false;
        // Detect whether or not the browser is IE
        var ieRegex = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (ieRegex.exec(ua) == null)
            this.exception = "The user agent detected does not contain Internet Explorer.";

        // Get the current "emulated" version of IE
        this.renderVersion = parseFloat(RegExp.$1);
        this.version = this.renderVersion;

        // Check the browser version with the rest of the agent string to detect compatibility mode
        if (ua.indexOf("Trident/7.0") > -1) {
            if (ua.indexOf("MSIE 7.0") > -1) {
                this.compatibilityMode = true;
            }
            this.version = 11;                  // IE 11
        }
        else if (ua.indexOf("Trident/6.0") > -1) {
            if (ua.indexOf("MSIE 7.0") > -1) {
                this.compatibilityMode = true;
            }
            this.version = 10;                  // IE 10
        }
        else if (ua.indexOf("Trident/5.0") > -1) {      
            if (ua.indexOf("MSIE 7.0") > -1) {
                this.compatibilityMode = true;
            }
            this.version = 9;                   // IE 9
        }
        else if (ua.indexOf("Trident/4.0") > -1) {
            if (ua.indexOf("MSIE 7.0") > -1) {
                this.compatibilityMode = true;
            }
            this.version = 8;                   // IE 8
        }
        else if (ua.indexOf("MSIE 7.0") > -1)
            this.version = 7;                   // IE 7
        else
            this.version = 6;                   // IE 6
    }
};

// Initialize the ieUserAgent object
ieUserAgent.init();

$(document).ready(function() {
    if(ieUserAgent.compatibilityMode) {
        //do stuff
    }
    if(ieUserAgent.version == 6) {
        //do stuff
    }
});

【讨论】:

    【解决方案3】:

    IE11 删除了用户代理字符串的“MSIE”部分。它也不止于此 - navigator.appName 将返回 Netscapenavigator.product 返回 Gecko

    造成这种情况的可能原因是 IE11 已经赶上了现代 Web 标准,Microsoft 不希望它在整个 Web 上触发旧的 if(IE) { // shitty simpler website } 处理程序。他们希望它看到与 Chrome/Firefox 相同的全功能版本。

    【讨论】:

    • 不不,早在 IE 赶上 any 网络标准之前,他们就一直在这样做。看看骗子。
    • @LightnessRacesinOrbit 他们多年来一直在添加东西,但删除密钥 MSIE 位是 11 中的新功能。
    猜你喜欢
    • 2016-01-28
    • 2016-03-17
    • 2011-12-08
    • 1970-01-01
    • 2011-01-29
    • 2011-09-21
    • 2016-05-03
    • 2011-01-16
    • 1970-01-01
    相关资源
    最近更新 更多