向IE8用户隐藏所有内容的最佳方式是什么

What is best way to hide all content from IE8 users

本文关键字:最佳 方式 是什么 IE8 用户 隐藏所      更新时间:2024-02-22

所以我知道我实际上可以对我的所有帖子和其他内容执行display:none;

问题是,你无论如何都可以看到源代码中的内容(可以肯定的是,99.9999%的IE8用户甚至不知道源代码选项,但无论如何都想对源代码隐藏它…)。相反,我想做的是根本不得到任何代码,除了可能在页面和源代码中显示这样的东西:

<!--[if lte IE 8]>
    <h2>You should update your browser version or choose another. Otherwise the content will not be visible for Internet Explorer users or below.<h2>
<![endif]-->

(这是一个WordPress网站)

警告:小心使用Jack Zelig上面的答案,这会危及服务器的安全,因为它正在对解析用户代理的函数运行eval。用户可以访问的eval()可以通过在服务器上运行代码来利用。

这个问题在这里得到了更彻底的回答:

if (preg_match('/MSIE's(?P<v>'d+)/i', @$_SERVER['HTTP_USER_AGENT'], $B) && $B['v'] <= 8) {
    // Browsers IE 8 and below
    // Don't render regular template files to these users
    // e.g.: include 'update-your-browser.php'
} else {
    // All other browsers
    // Offer a version of the regular site
}

可以采用其他类似的解决方案:我可以用PHP检测IE6吗?

注意:另外,避免在正则表达式上使用/e,因为它也会计算您正在解析的任何代码中包含的任何代码。

简短的回答是肯定的,可以做到。

但就你所描述的内容而言,我想说,你永远不应该担心人们在你的源代码中看到的内容,除非这是一个安全问题(在这种情况下,它应该对所有人都隐藏起来,使用各种可能看到它的技术)。对于访问者来说,表示层才是真正重要的,在这种情况下,display:none就足够了。

在主题文件夹的index.php中试试这个:

<?php
function iever($compare=false, $to=NULL){
    if(!preg_match('/MSIE (.*?);/', $_SERVER['HTTP_USER_AGENT'], $m)
     || preg_match('#Opera#', $_SERVER['HTTP_USER_AGENT']))
        return false === $compare ? false : NULL;
    if(false !== $compare
        && in_array($compare, array('<', '>', '<=', '>=', '==', '!=')) 
        && in_array((int)$to, array(5,6,7,8,9,10))){
        return eval('return ('.$m[1].$compare.$to.');');
    }
    else{
        return (int)$m[1];
    }
}
if(iever('<=', 8)){ 
  <?php include (TEMPLATEPATH . '/shared/header.php' ); ?>
  <!-- YOUR CONTENT -->
  <?php 
    include (TEMPLATEPATH . '/shared/sidebar.php' );
    include (TEMPLATEPATH . '/shared/footer.php' ); 
  ?>
} else {
  echo("Oh noes. IE8 or below. Maybe");
}

如果这样做有效,请在评论中告诉我,我会找出要挂接的操作(也许是init?),这样就可以对每个页面进行挂接。

我从这里得到了浏览器嗅探功能。

你知道浏览器嗅探很糟糕,对吧?