如何识别IE8及以下版本以发布唯一代码?

Vin*_*tto 0 html php regex conditional-comments internet-explorer-8

我打算将一些代码放入我的CMS标题中,该标题将提醒任何使用IE 8及以下版本的用户,建议升级他们的浏览器.

在PHP中,我有以下代码,它显示所有版本的IE.

<?php
    if(preg_match('/(?i)msie [1-8]/',$_SERVER['HTTP_USER_AGENT'])) {
        // if IE<=8
        ?>
            <!--[if lte IE 8]>
            <div class="ieNotice">
                <a href="http://windows.microsoft.com/en-us/internet-explorer/" target="_blank">
                    <img src="/skin/frontend/default/hellowired/images/ieUpgrade.jpg">
                </a>
            </div>
            <style>
                .ieNotice {
                    background-color: #eee;
                    padding: 10px;
                }
            </style>
            <![endif]-->
        <?php
    }
?>
Run Code Online (Sandbox Code Playgroud)

这应该是两倍的工作.一,$_SERVER['HTTP_USER_AGENT'])应该能够告诉浏览器分开.但是,我注意到当我运行跨浏览器测试时,IE 8和UP将返回相同的结果$_SERVER['HTTP_USER_AGENT']),即:

Mozilla/4.0(兼容; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)

好的......即使这不起作用......"Quirks模式"条件评论不会作为后备只显示图像并链接到IE8及以下? 我在这里错过了什么?

编辑

好的,所以我按照指示完成了.我删除了PHP代码,并将.ieNotice该类放在display: none属性中.现在,在这里包含的样式表上:

<!--[if lte IE 8]>
<link rel="stylesheet" type="text/css" href="/skin/frontend/default/hellowired/css/styles-ie-lte8.css" media="all" />
<![endif]-->
Run Code Online (Sandbox Code Playgroud)

我添加了以下行:

.ieNotice { display: block !important;}
Run Code Online (Sandbox Code Playgroud)

我已经在Firefox和Chrome上正确地看到了这项工作,但它仍然显示了我为IE 9,10和11创建的横幅.

Sam*_*Sam 5

您不应该依赖用户代理字符串,这可能会被用户欺骗.特别是因为你已经在使用<!--[if lte IE 8]>,只会出现IE <= 8.我完全摆脱了PHP条件.