0

如果他们正在使用 Internet Explorer,我试图将用户重定向到另一个页面,但此代码不会重定向他们,它会在 ie 上正常加载页面

我尝试了不同的 MSIE 变体,但似乎没有任何效果

session_start();

  if (strpos($_SERVER['HTTP_USER_AGENT'], '/MSIE/i') !== false){
header('Location: /ie.php');
die();
}else{
echo "User Agent not recognised.";
}

有人有想法么?

4

4 回答 4

6

试试这个:

session_start();

if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false){
    header('Location: /ie.php');
    die();
}else{
    echo "User Agent not recognised.";
}

原因是strpos' 的第二个参数,needle,不应该是正则表达式。

于 2012-10-12T13:53:56.050 回答
1

还有另一种根本不需要 php 的方法,它是条件注释:

<!--[if IE]>
<?php echo "User is using Internet Explorer"; ?>
<![endif]-->

<!--[if IE 6]>
<?php echo "User is using Internet Explorer 6"; ?>
<![endif]-->

ETC...

如果资源管理器是 IE 并且版本小于或大于 X,您也可以获得:

Code if User uses IE and it's below version 9 Code if User uses IE and it's below than or equal to version 7 Code if User uses IE and it's greater than version 6

The advantages are that you can also pick the version of the browser, easy to implement, and you are using a little less resources from your server xD (although it would be a insignificant difference).

Another advantage is that you can use that to include css or js files in the header of your page depending on the browser.

The disadvantage is that you has less control as it's browser based(client side).

于 2012-10-12T14:03:58.923 回答
0

换成'/MSIE/i'简单MSIE试试。另外,你需要 die() 函数吗?

于 2012-10-12T13:54:52.893 回答
-2

试着把ob_start(); 在 PHP 文件的顶部

于 2012-10-12T13:54:25.550 回答