IE 9用户代理正在撒谎!我见过的最奇怪的事

时间:2012-03-30 18:35:30

标签: php internet-explorer

所以这是我的窘境。我有一个对用户代理非常敏感的网站。将正确的用户代理发送到站点或该站点不适用于该用户是绝对比较的。网站工作正常,除非通过永久重定向将用户定向到那里。

它变得有趣的地方。看,Firefox和Chrome都没有问题。但IE开始谎称你看到状态和域名更改时使用的浏览器版本。我有一个客户端(一个非常大的慢客户端)通过永久重定向从外部链接我的网站。而且我不认为这个客户现在有能力改变它。因此我坚持使用它,当IE出现时,我的浏览器检测变得很糟糕:

  

Mozilla / 5.0(兼容; MSIE 9.0; Windows NT 6.1; WOW64; Trident / 5.0)

为:

  

Mozilla / 4.0兼容; MSIE 7.0; Windows NT 6.1; WOW64;三叉戟/ 5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3

我尝试过的一些事情: 添加了更改渲染引擎的元标记:

<meta content="IE=7" http-equiv="X-UA-Compatible" />

在IE 8中工作,但9忽略它。 尝试检测.net框架的不同版本,但它并不总是可靠的。 在Javascript中使用canvas标签尝试,但只在IE 7中打破它,有时甚至是8。

我认为必须有某种标题我可以推动它,但我不知道任何标题。

有没有人见过这个?有功能性的解决方法吗?

修改 有人建议现代化。这是一个很棒的主意。

我解决这个问题的另一件事是重写我的浏览器检测脚本。

在这里,如果有人发现自己遇到类似的问题: 它很脏,但它有效。

function getBrowser() {

    //shamlessly "borrowed" from the manual at http://php.net/manual/en/function.get-browser.php
    //Needed some cleanup.
    //Still needs some cleanup

    $u_agent = $_SERVER['HTTP_USER_AGENT'];
    $bname = 'Unknown';
    $platform = 'Unknown';
    $version = "";
    $xploded = explode(';',$u_agent);

    //pretty($xploded);

    // finally get the correct version number
    $known = array('Version', $ub, 'other');
    $pattern = '#(?<browser>' . join('|', $known) .
           ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
    if (!preg_match_all($pattern, $u_agent, $matches)) {
      // we have no matching number just continue
    }

    // see how many we have
    $i = count($matches['browser']);
    if ($i != 1) {
      //we will have two since we are not using 'other' argument yet
      //see if version is before or after the name
      if (strripos($u_agent, "Version") < strripos($u_agent, $ub)) {
        $version = $matches['version'][0];
      } else {
        $version = $matches['version'][1];
      }
    } else {
      $version = $matches['version'][0];
    }


    // check if wfunction getBrowser() {
    $u_agent = $_SERVER['HTTP_USER_AGENT'];
    $bname = 'Unknown';
    $platform = 'Unknown';
    $version = "";

    //First get the platform?
    if (preg_match('/linux/i', $u_agent)) {
      $platform = 'linux';
    } elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
      $platform = 'mac';
    } elseif (preg_match('/windows|win32/i', $u_agent)) {
      $platform = 'windows';
    }

    // Next get the name of the useragent yes seperately and for good reason
    if (preg_match('/MSIE/i', $u_agent) && !preg_match('/Opera/i', $u_agent)) {
      $bname = 'Internet Explorer';
      $ub = "MSIE";
    } elseif (preg_match('/Firefox/i', $u_agent)) {
      $bname = 'Mozilla Firefox';
      $ub = "Firefox";
    } elseif (preg_match('/Chrome/i', $u_agent)) {
      $bname = 'Google Chrome';
      $ub = "Chrome";
    } elseif (preg_match('/Safari/i', $u_agent)) {
      $bname = 'Apple Safari';
      $ub = "Safari";
    } elseif (preg_match('/Opera/i', $u_agent)) {
      $bname = 'Opera';
      $ub = "Opera";
    } elseif (preg_match('/Netscape/i', $u_agent)) {
      $bname = 'Netscape';
      $ub = "Netscape";
    }

    // finally get the correct version number
    $known = array('Version', $ub, 'other');
    $pattern = '#(?<browser>' . join('|', $known) .
           ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
    if (!preg_match_all($pattern, $u_agent, $matches)) {
      // we have no matching number just continue
    }

    // see how many we have
    $i = count($matches['browser']);
    if ($i != 1) {
      //we will have two since we are not using 'other' argument yet
      //see if version is before or after the name
      if (strripos($u_agent, "Version") < strripos($u_agent, $ub)) {
        $version = $matches['version'][0];
      } else {
        $version = $matches['version'][1];
      }
    } else {
      $version = $matches['version'][0];
    }
    // check if we have a number
    if ($version == null || $version == "") {
      $version = "?";
    }

    $bname = strtolower($bname);
    $windows_version = floatval(trim(str_ireplace('Windows NT','',$xploded[2]) ));

    //Sloppy hack for dealing with IE 9, specifically.
    $trial = intval($version);
    if($windows_version == 6.1 && $bname == 'internet explorer' && $trial == 7){
        //the browser is lying to you.
        //print "We are being lied to<br>";
        $version = 9.0;
    }

    //print 'windows version is......'.$windows_version.'<br>';
    //print 'browser id is......'.$bname.'<br>';
    //print 'raw version is......'.$version.'<br>';


    return array(
        'test'=>'test',
        'name' => $bname,
        'version' => intval($version)
    );
}

1 个答案:

答案 0 :(得分:3)

不幸的是,由于你继承了一个原本做错事的网站,你将不得不慢慢开始将功能部分移植到使用特征检测而不是UA嗅探。

我建议你安装Modernizer并慢慢开始移植。从最破碎的页面开始,然后从那里开始。