php标头位置重定向不起作用 [英] Php header location redirect not working

查看:72
本文介绍了php标头位置重定向不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道为什么这不起作用.这是代码:

No idea why this is not working. Here is the code:

if ((isset($_POST['cancel'])) && ($_POST['cancel'] == 'cancel'))
{
    header('Location: page1.php');
    echo $_POST['cancel'];
}

此输出的cancel不会重定向页面,而是重定向到网页.它跳过了重定向.为什么?我怎样才能解决这个问题? page1.php是一个实际页面,与当前页面位于同一文件夹中.上面的代码是php文件的第一行.没什么没有.甚至没有空格.

Instead of redirecting the page, this output's cancel to the webpage. It skipped over the redirect. Why? How can I fix this? page1.php is a real page located in the same folder as the current page. The above code is the very first lines of the php file. Nothing before it. Nothing. Not even whitespace.

推荐答案

Pekka在评论中回答了我的问题.他没有发布答案,所以我现在是.标头重定向后,请使用exit()方法.由于某种原因,页眉的其余代码在header()方法重定向后仍继续执行.执行其余代码后,echo语句将输出到页面.输出到页面后,您将无法使用标题功能进行重定向.为避免执行其余代码,请使用exit().谢谢Pekka.

Pekka answered my question in the comments. He didn't post an answer, so I am now. Use the exit() method after the header redirect. For some reason the rest of the code of the page continues to execute after the header() method redirect. When the rest of the code executes, the echo statement is outputted to the page. And you can't redirect using the header function after you output to the page. To avoid rest of the code from executing, use exit(). Thanks Pekka.

更新:使用Web浏览器Internet Explorer时,我注意到$ _POST ['cancel']不可靠.我不确定为什么会这样,但是我怀疑IE在表单提交中发布了其他变量,特别是变量取消"已发布.我通过使用'cancel'以外的变量名解决了这个问题.结合使用exit()和唯一的变量名对我来说很有效.

UPDATE: When using the web browser Internet Explorer, I have noticed that $_POST['cancel'] is not reliable. I am not exactly sure why this is, but I suspect IE posts additional variables on a form submit, specifically the variable 'cancel' is posted. I solved this by using a variable name other than 'cancel'. The combination of using exit() and a unique variable name is working for me.

这篇关于php标头位置重定向不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆