如何在更改用户代理时检测到真正的IE

时间:2015-11-30 04:28:43

标签: php internet-explorer user-agent

在我的项目中使用PHP来检测浏览器,但我的客户使用IE11并将用户代理更改为IE7。我的客户要求检测的是IE11,而不是IE7IE 11

请帮我检测IE11

1 个答案:

答案 0 :(得分:0)

使用JS检查

function checkIE()
    {
        var ieVr = -1;
        if (navigator.appName == 'Microsoft Internet Explorer')
        {
            var agent = navigator.userAgent;
            var exp = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
            if (exp.exec(agent) != null)
                ieVr = parseFloat(RegExp.$1);
        }
        else if (navigator.appName == 'Netscape')
        {
            var agent = navigator.userAgent;
            var exp = new RegExp("Trident/.*ieVr:([0-9]{1,}[\.0-9]{0,})");
            if (exp.exec(agent) != null)
                ieVr = parseFloat(RegExp.$1);
        }
        return ieVr;
    }

根据PHP中的Doug

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
    }   
}