我可以用 PHP 检测 IE6 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/671890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:26:09  来源:igfitidea点击:

Can I detect IE6 with PHP?

phpinternet-explorer-6cross-browser

提问by PHLAK

Is there a way to use PHP to detect if the page is being loaded using IE6?

有没有办法使用 PHP 来检测页面是否正在使用 IE6 加载?

回答by Jeremy Ruten

Try checking their user agent for 'MSIE 6.'.

尝试检查他们的用户代理'MSIE 6.'

$using_ie6 = (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.') !== FALSE);

This is based on this user agent information.

这是基于此用户代理信息

回答by ólafur Waage

You can detect IE6 with HTML this way

您可以通过这种方式使用 HTML 检测 IE6

<!--[if IE 6]>
// ie6 only stuff here
<![endif]-->

Here's a link on how it's done in PHPWay Back Machinebut I've seen many false positives in parsing the $_SERVER["HTTP_USER_AGENT"]for IE6

这是有关如何在 PHP Way Back Machine 中完成的链接,但我在解析$_SERVER["HTTP_USER_AGENT"]IE6 时看到了许多误报

回答by thomasrutter

Many of the user-agent based answers on this page aren't too reliable because Opera often identifies itself with a user-agent string containing "MSIE 6.0", such as:

此页面上的许多基于用户代理的答案都不太可靠,因为 Opera 经常使用包含“MSIE 6.0”的用户代理字符串来标识自己,例如:

Mozilla/4.0 (compatible; MSIE 6.0; X11; Linux i686; en) Opera 9.51

This affects all versions of Opera 5 through 9 and even Opera 10 and can be turned on or off from within Opera. See this page.

这会影响 Opera 5 到 9 甚至 Opera 10 的所有版本,并且可以从 Opera 中打开或关闭。 请参阅此页面

A common approach I've seen is to test for "MSIE" and against "Opera". For example,

我见过的一种常见方法是测试“MSIE”和“Opera”。例如,

if (preg_match('/\bmsie 6/i', $ua) && !preg_match('/\bopera/i', $ua))
  echo "We have IE6!";

回答by marverix

well PHLAK...

好吧,PHLAK...

i think this one is much better :P

我觉得这个好多了:P

  if(preg_match('/msie [2-6]/i',$_SERVER['HTTP_USER_AGENT'])) {
   // if IE<=6
  } else {
   //if IE>6
  }

回答by broox

Like everyone else said, there willbe false positives by just checking the user agent... so why not use both, user agent checking and a conditional comment.

像其他人一样说,有误报的只是检查用户代理...那么,为什么不使用这两种,用户代理检查和条件注释。

for example...

例如...

<? if (preg_match('/\bmsie 6/i', $_SERVER['HTTP_USER_AGENT']) { ?>
  <!--[if IE 6]>
    // ie6 only stuff here
  <![endif]-->
<? } ?>

This way you won't be sending back this unnecessary code to most browsers... but will still be safe in case of a false positive.

这样你就不会将这个不必要的代码发送回大多数浏览器......但在误报的情况下仍然是安全的。

回答by bobince

You can, using the HTTP User-Agent header, but I'd strongly advise not doing that if possible. The User-Agent header is very very difficult to parse accurately, and tends towards false positives with simple string matching?—?even ignoring the issue of browsers that pretend to be other browsers. For example Jeremy's “MSIE 6.” string will match IE Mobile, which is so very different from IE6 that you generally don't want to conflate them.

可以使用 HTTP User-Agent 标头,但我强烈建议您尽可能不要这样做。User-Agent 标头很难准确解析,并且容易通过简单的字符串匹配出现误报? - 甚至忽略冒充其他浏览器的浏览器的问题。例如 Jeremy 的“MSIE 6”。string 将匹配 IE Mobile,这与 IE6 非常不同,您通常不想将它们混为一谈。

Plus when you send different HTML to different browsers, you have to use the ‘Vary' header (which makes caches less effective) to avoid that caches send the wrong pages to different browsers.

另外,当您将不同的 HTML 发送到不同的浏览器时,您必须使用“Vary”标头(这会降低缓存的效率)以避免缓存将错误的页面发送到不同的浏览器。

So if you can find another place to do the browser differentiation that's definitely best. ólafur's approach with conditional comments is usually the simplest approach for changing JavaScript and HTML markup/CSS links.

因此,如果您能找到另一个地方进行浏览器差异化,那绝对是最好的。ólafur 的条件注释方法通常是更改 JavaScript 和 HTML 标记/CSS 链接的最简单方法。

回答by Alex

You can use it for many browsers, but it is time consuming and sometimes false positive...

您可以将它用于许多浏览器,但它很耗时,有时还会误报...

function do_stylesheet_link() {
// Picks up appropriate css file depending on the user-agent

//  if ( stristr($_SERVER['HTTP_USER_AGENT'], 'Firefox') ) {
//echo '<link rel="stylesheet" href="css/firefox.css" type="text/css" />'; }    

//elseif ( stristr($_SERVER['HTTP_USER_AGENT'], 'Chrome') ) {
//echo '<link rel="stylesheet" href="css/chrome.css" type="text/css" />'; }

//elseif ( stristr($_SERVER['HTTP_USER_AGENT'], 'Safari') ) {
//echo '<link rel="stylesheet" href="css/safari.css" type="text/css" />'; }

//elseif ( stristr($_SERVER['HTTP_USER_AGENT'], 'Opera') ) {
//echo '<link rel="stylesheet" href="css/opera.css" type="text/css" />'; }

    if ( stristr($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.') )  {
echo '<link rel="stylesheet" href="css/ie6.css" type="text/css" />'; }

elseif ( stristr($_SERVER['HTTP_USER_AGENT'], 'MSIE 7.') ) {
echo '<link rel="stylesheet" href="css/ie7.css" type="text/css" />'; }

elseif ( stristr($_SERVER['HTTP_USER_AGENT'], 'MSIE 8.') ) {
echo '<link rel="stylesheet" href="css/ie8.css" type="text/css" />'; }

else {
echo '<link rel="stylesheet" href="css/style.css" type="text/css" />';
}

}

}

回答by Bongo

if(substr($_SERVER['HTTP_USER_AGENT'],0,34)=="Mozilla/4.0 (compatible; MSIE 6.0;") echo "I am stupid IE6";

if(substr($_SERVER['HTTP_USER_AGENT'],0,34)=="Mozilla/4.0 (compatible; MSIE 6.0;") echo "I am stupid IE6";

IE 6 Always starts with "Mozilla/4.0 (compatible; MSIE 6.0;" IE 7,8 have "MSIE 6" in the middle...

IE 6 总是以“Mozilla/4.0(兼容;MSIE 6.0;”)开头,IE 7,8 中间有“MSIE 6”...

回答by AndreyP

function isIE($versions=array()) {
    if (!empty($versions))
        $versions = '('.implode('|',$versions).')';
    else
        $versions = '1?\d';
    $ua = $_SERVER['HTTP_USER_AGENT'];
    $is_not_opera = false===stripos($ua, 'opera');
    return $is_not_opera && preg_match('/\bmsie '.$versions.'\./i', $ua);
}

Using:

使用:

$is_ie = isIE(); //Any version
$is_ie_7 = isIE(array(7)); //IE 7
$is_old_id = isIE(array(6,7));//IE 7 and 8