Php 标头位置重定向不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21226166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Php header location redirect not working
提问by John S
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'];
}
Instead of redirecting the page, this output's cancelto 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.
此输出不是重定向页面,而是cancel指向网页。它跳过了重定向。为什么?我怎样才能解决这个问题?page1.php 是与当前页面位于同一文件夹中的真实页面。上面的代码是php文件的第一行。在它之前什么都没有。没有。甚至没有空格。
采纳答案by John S
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.
Pekka 在评论中回答了我的问题。他没有发布答案,所以我现在是。exit()在标头重定向之后使用该方法。出于某种原因,页面的其余代码在 header() 方法重定向后继续执行。当其余代码执行时,echo 语句被输出到页面。并且您无法在输出到页面后使用 header 函数进行重定向。为避免执行其余代码,请使用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.
更新:使用网络浏览器 Internet Explorer 时,我注意到 $_POST['cancel'] 不可靠。我不确定为什么会这样,但我怀疑 IE 在提交表单时发布了其他变量,特别是发布了变量“取消”。我通过使用“取消”以外的变量名解决了这个问题。使用 exit() 和唯一变量名的组合对我有用。
回答by Mihai Stancu
This is likely a problem generated by the headers being already sent.
这可能是由已经发送的标头产生的问题。
Why
为什么
This occurs if you have echoed anything before deciding to redirect. If so, then the initial (default) headers have been sent and the new headers cannot replace something that's already in the output buffer getting ready to be sent to the browser.
如果您在决定重定向之前回显了任何内容,则会发生这种情况。如果是这样,则初始(默认)标头已发送,新标头无法替换输出缓冲区中已准备好发送到浏览器的内容。
Sometimes it's not even necessary to have echoed something yourself:
有时甚至没有必要自己回应一些东西:
- if an error is being outputted to the browser it's also considered content so the headers must be sent before the error information;
- if one of your files is encoded in one format (let's say ISO-8859-1) and another is encoded in another (let's say UTF-8 with BOM) the incompatibility between the two encodings may result in a few characters being outputted;
- 如果错误被输出到浏览器,它也被视为内容,因此必须在错误信息之前发送标头;
- 如果您的一个文件以一种格式编码(假设为 ISO-8859-1)而另一个文件以另一种格式编码(假设为带有 BOM 的 UTF-8),则两种编码之间的不兼容可能会导致输出几个字符;
Let's check
让我们检查
To test if this is the case you have to enable error reporting: error_reporting(E_ALL);and set the errors to be displayed ini_set('display_errors', TRUE);after which you will likely see a warning referring to the headers being already sent.
要测试是否是这种情况,您必须启用错误报告:error_reporting(E_ALL);并设置要显示的错误,ini_set('display_errors', TRUE);之后您可能会看到一个警告,指的是已发送的标头。
Let's fix
让我们修复
Fixing this kinds of errors:
修复此类错误:
- writing your redirect logic somewhere in the code before anything is outputted;
- using output buffers to trap any outgoing info and only release it at some point when you know all redirect attempts have been run;
- Using a proper MVC framework they already solve it;
- 在输出任何内容之前,在代码中的某处编写重定向逻辑;
- 使用输出缓冲区来捕获任何传出信息,并且仅在您知道所有重定向尝试都已运行时才将其释放;
- 使用适当的 MVC 框架,他们已经解决了;
More
更多的
MVC solves it both functionally by ensuring that the logic is in the controller and the controller triggers the display/rendering of a view only at the end of the controllers. This means you can decide to do a redirect somewhere within the action but not withing the view.
MVC 通过确保逻辑在控制器中并且控制器仅在控制器的末尾触发视图的显示/渲染来在功能上解决它。这意味着您可以决定在动作内的某处进行重定向,但不在视图内进行。
回答by Kiel Labuca
I have experienced that kind of issue before and now I'm not using header('Location: pageExample.php');anymore, instead I'm using javascript's document.location.
我以前遇到过这种问题,现在我不再使用header('Location: pageExample.php');了,而是使用 javascript 的document.location.
Change your:
改变你的:
header('Location: page1.php');
To something like this:
对于这样的事情:
echo "<script type='text/javascript'> document.location = 'page1.php'; </script>";
And what is the purpose of echo $_POST['cancel'];by the way?, just delete that line if what you want is just the redirection. I've been using that <script>every time and it doesn't fail me. :-)
echo $_POST['cancel'];顺便说一句,目的是什么?,如果您想要的只是重定向,请删除该行。我<script>每次都在使用它,它并没有让我失望。:-)
回答by Nathan Srivi
Use @obstartor try to use Java Script
使用@obstart或尝试使用Java Script
put your obstart();into your top of the page
把你的obstart();放在页面的顶部
if ((isset($_POST['cancel'])) && ($_POST['cancel'] == 'cancel'))
{
header('Location: page1.php');
exit();
}
If you use JavascriptUse window.location.href
如果您使用Javascript使用 window.location.href
window.location.href example:
window.location.href 示例:
if ((isset($_POST['cancel'])) && ($_POST['cancel'] == 'cancel'))
{
echo "<script type='text/javascript'>window.location.href = 'page1.php';</script>"
exit();
}
回答by IRSHAD
I had also the similar issue in godaddy hosting. But after putting ob_start(); at the beginning of the php page from where page was redirecting, it was working fine.
我在 Godaddy 托管中也遇到了类似的问题。但是在放入 ob_start(); 之后 在页面重定向的 php 页面的开头,它工作正常。
Please find the example of the fix:
请找到修复的示例:
fileName:index.php
文件名:index.php
<?php
ob_start();
...
header('Location: page1.php');
...
ob_end_flush();
?>
回答by VishnuKumar Pv
I had similar problem...
solved by adding ob_start();and ob_end_flush();...
我有类似的问题...通过添加ob_start();和 解决ob_end_flush();...
<?php
ob_start();
require 'engine/vishnuHTML.class.php';
require 'engine/admin/login.class.php';
$html=new vishnuHTML();
(!isset($_SESSION))?session_start():"";
/* blah bla Code
...........
...........
*/
</div>
</div>
<?php
}
ob_end_flush();
?>
Think of ob_start() as saying "Start remembering everything that would normally be outputted, but don't quite do anything with it yet."
把 ob_start() 想象成说“开始记住通常会输出的所有内容,但还没有对它做任何事情。”
ob_end_clean() or ob_flush(), which either stops saving things and discards whatever was saved, or stops saving and outputs it all at once, respectively.
ob_end_clean() 或 ob_flush(),它们要么停止保存内容并丢弃已保存的内容,要么停止保存并立即输出所有内容。
回答by Hariharaselvam Balasubramanian
For me also it was not working. Then i try with javascript inside php like
对我来说也是行不通的。然后我尝试在 php 中使用 javascript,例如
echo "<script type='text/javascript'> window.location='index.php'; </script>";
This will definitely working.
这肯定会奏效。
回答by Niketan Raval
Neer to specify exit code here so php not execute further
需要在此处指定退出代码,以便 php 不会进一步执行
if ((isset($_POST['cancel'])) && ($_POST['cancel'] == 'cancel'))
{
header('Location: page1.php');
exit(0); // require to exit here
}
回答by Raugaral
Try adding
尝试添加
ob_start();
ob_start();
at the top of the code i.e. before the include statement.
在代码的顶部,即在 include 语句之前。
回答by Abhi Burk
Make Sure that you don't leave a space before <?phpwhen you start <?phptag at the top of the page.
确保在页面顶部<?php开始<?php标记之前没有留下空格。

