简体   繁体   English

PHP Internet Explorer重定向标头不起作用

[英]php internet explorer redirect header not working

im trying to redirect users to another page if they are using internet explorer but this code doen't redirect them it loads the page up as normal on ie 我试图将用户重定向到另一个页面(如果他们使用的是Internet Explorer),但是此代码不会将他们重定向到该页面,即按正常方式加载该页面

I have tried different variations of MSIE but nothing seems to work 我尝试了MSIE的不同变体,但似乎无济于事

session_start();

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

any one have any ideas? 有人有想法么?

try this: 尝试这个:

session_start();

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

The reason is that strpos 's second parameter, the needle, is not supposed to be a regular expression. 原因是strpos的第二个参数,needle,不应该是正则表达式。

There is another way in which you don't need php at all, and it's conditional comments: 还有一种根本不需要php的方式,它是有条件的注释:

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

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

etc... 等等...

Also you can get if the explorer is IE and if the version is lesser or greater than X: 如果资源管理器是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 如果用户使用IE且低于版本9的代码如果用户使用IE且低于或等于版本7的代码如果用户使用IE且高于版本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). 优点是,您还可以选择易于实现的浏览器版本,并且从服务器xD使用的资源要少一些(尽管差异不大)。

Another advantage is that you can use that to include css or js files in the header of your page depending on the browser. 另一个优点是您可以使用它在页面的标题中包含css或js文件,具体取决于浏览器。

The disadvantage is that you has less control as it's browser based(client side). 缺点是基于浏览器(客户端)的控制较少。

Change '/MSIE/i' to simply MSIE and try it. '/MSIE/i'更改为简单的MSIE并尝试。 Also, do you need the die() function? 另外,您是否需要die()函数?

try to put ob_start(); 尝试把ob_start(); in top of PHP file 在PHP文件顶部

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM