PHP:如果 Internet Explorer 6、7、8 或 9

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5302302/
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-25 20:55:35  来源:igfitidea点击:

PHP: If internet explorer 6, 7, 8 , or 9

phpbrowser

提问by Cameron

I want to do a conditional in PHP for the different versions of Internet Explorer along the lines of:

我想在 PHP 中为不同版本的 Internet Explorer 执行以下操作:

if($browser == ie6){ //do this} elseif($browser == ie7) { //dothis } elseif...

if($browser == ie6){ //do this} elseif($browser == ie7) { //dothis } elseif...

I have seen many variations on similar code, but looking for something super simple that is very easy to code to do some simple if and else and do different things.

我已经看到类似代码的许多变体,但正在寻找一些非常简单的代码来执行一些简单的 if 和 else 并做不同的事情。

Thanks

谢谢

EDIT: I need this to show some different messages to users so CSS conditionals etc are no good.

编辑:我需要这个来向用户显示一些不同的消息,所以 CSS 条件等不好。

回答by Cameron

This is what I ended up using a variation of, which checks for IE8 and below:

这就是我最终使用的变体,它检查 IE8 及以下:

if (preg_match('/MSIE\s(?P<v>\d+)/i', @$_SERVER['HTTP_USER_AGENT'], $B) && $B['v'] <= 8) {
    // Browsers IE 8 and below
} else {
    // All other browsers
}

回答by Doug

A version that will continue to work with both IE10 and IE11:

将继续与 IE10 和 IE11 一起使用的版本:

preg_match('/MSIE (.*?);/', $_SERVER['HTTP_USER_AGENT'], $matches);
if(count($matches)<2){
  preg_match('/Trident\/\d{1,2}.\d{1,2}; rv:([0-9]*)/', $_SERVER['HTTP_USER_AGENT'], $matches);
}

if (count($matches)>1){
  //Then we're using IE
  $version = $matches[1];

  switch(true){
    case ($version<=8):
      //IE 8 or under!
      break;

    case ($version==9 || $version==10):
      //IE9 & IE10!
      break;

    case ($version==11):
      //Version 11!
      break;

    default:
      //You get the idea
  }
}

回答by Daff

You can check the HTTP_USER_AGENT server variable. The user agent transfered by IE contains MSIE

您可以检查 HTTP_USER_AGENT 服务器变量。IE传输的用户代理包含MSIE

if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) { ... }

For specific versions you can extend your condition

对于特定版本,您可以扩展您的条件

if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.') !== false) { ... }

Also see this related question.

另请参阅此相关问题

回答by Michael Jasper

Here is a great resource for detecting browsers in php: http://php.net/manual/en/function.get-browser.php

这是在 php 中检测浏览器的一个很好的资源:http: //php.net/manual/en/function.get-browser.php

Here is one of the examples that seems the simplest:

这是看起来最简单的示例之一:

<?php
function get_user_browser()
{
    $u_agent = $_SERVER['HTTP_USER_AGENT'];
    $ub = '';
    if(preg_match('/MSIE/i',$u_agent))
    {
        $ub = "ie";
    }
    elseif(preg_match('/Firefox/i',$u_agent))
    {
        $ub = "firefox";
    }
    elseif(preg_match('/Safari/i',$u_agent))
    {
        $ub = "safari";
    }
    elseif(preg_match('/Chrome/i',$u_agent))
    {
        $ub = "chrome";
    }
    elseif(preg_match('/Flock/i',$u_agent))
    {
        $ub = "flock";
    }
    elseif(preg_match('/Opera/i',$u_agent))
    {
        $ub = "opera";
    }

    return $ub;
}
?>

Then later in your code you could say something like

然后在你的代码中你可以说类似的话

$browser = get_user_browser();
if($browser == "ie"){
    //do stuff
}

回答by Vince Lowe

I do this

我这样做

$u = $_SERVER['HTTP_USER_AGENT'];

$isIE7  = (bool)preg_match('/msie 7./i', $u );
$isIE8  = (bool)preg_match('/msie 8./i', $u );
$isIE9  = (bool)preg_match('/msie 9./i', $u );
$isIE10 = (bool)preg_match('/msie 10./i', $u );

if ($isIE9) {
    //do ie9 stuff
}

回答by Mike S

PHP has a function called get_browser()that will return an object (or array if you so choose) with details about the users' browser and what it can do.

PHP 有一个名为get_browser()的函数,该函数将返回一个对象(或数组,如果您选择的话),其中包含有关用户浏览器及其功能的详细信息。

A simple look through gave me this code:

一个简单的浏览给了我这个代码:

$browser = get_browser( null, true );
if( $browser['name'] == "Firefox" )
    if( $browser['majorversion'] == 4 )
        echo "You're using Firefox version 4!";

This is not a surefire way (as it reads from HTTP_USER_AGENT, which can be spoofed, and will sometimes be analyzed wrong by php), but it's the easiest one that you can find as far as I know.

这不是一个万无一失的方式(因为它从HTTP_USER_AGENT读取,它可以被欺骗,有时会被 php 分析错误),但据我所知,这是你能找到的最简单的方法。

回答by xsor

if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/(?i)msie|trident|edge/",$_SERVER['HTTP_USER_AGENT'])) {
// eh, IE found
}

回答by minorgod

Here's a little php function I wrote that uses the regex directly from MSFT's suggested javascript sniffing code from this article: http://msdn.microsoft.com/en-us/library/ms537509(v=vs.85).aspx

这是我编写的一个小 php 函数,它直接使用来自本文的 MSFT 建议的 javascript 嗅探代码中的正则表达式:http: //msdn.microsoft.com/en-us/library/ms537509(v=vs.85).aspx

/**
* Returns the version of Internet Explorer or false
*/
function isIE(){

    $isIE = preg_match("/MSIE ([0-9]{1,}[\.0-9]{0,})/",$_SERVER['HTTP_USER_AGENT'],$version);
    if($isIE){
        return $version[1];
    }
    return $isIE;

}

回答by jolt

You can as well look into PHP's get_browser();http://php.net/manual/en/function.get-browser.php

您也可以查看 PHP 的get_browser();http://php.net/manual/en/function.get-browser.php

Maybe you'll find it useful for more features.

也许您会发现它对更多功能很有用。

回答by Markus Zeller

Checking for MSIE only is not enough to detect IE. You need also "Trident" which is only used in IE11. So here is my solution which worked an versions 8 to 11.

仅检查 MSIE 不足以检测 IE。您还需要仅在 IE11 中使用的“Trident”。所以这是我的解决方案,它适用于版本 8 到 11。

$agent=strtoupper($_SERVER['HTTP_USER_AGENT']);
$isIE=(strpos($agent,'MSIE')!==false || strpos($agent,'TRIDENT')!==false);