PHP标头位置重定向不起作用

时间:2022-03-14 15:25:28

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 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.

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

13 个解决方案

#1


17  

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通过确保逻辑在控制器中并且控制器仅在控制器的末尾触发视图的显示/呈现来在功能上解决它。这意味着您可以决定在操作中的某个位置执行重定向,但不能使用视图。

#2


13  

Use @obstart or try to use Java Script

使用@obstart或尝试使用Java Script

put your obstart(); into your top of the page

把你的障碍();进入页面顶部

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

If you use Javascript Use window.location.href

如果您使用Javascript使用window.location.href

window.location.href example:

 if ((isset($_POST['cancel'])) && ($_POST['cancel'] == 'cancel'))
    {
        echo "<script type='text/javascript'>window.location.href = 'page1.php';</script>"
        exit();
    }

#3


10  

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.

我之前遇到过这种问题,现在我没有使用标题('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']的目的是什么?顺便说一下?如果您想要的只是重定向,只需删除该行。我每次都在使用那个

#4


7  

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

<?php
ob_start();
...
header('Location: page1.php');
...
ob_end_flush();
?>

#5


5  

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.

这肯定会奏效。

#6


3  

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.

佩卡在评论中回答了我的问题。他没有回复,所以我现在。在标题重定向后使用exit()方法。由于某种原因,页面的其余代码在header()方法重定向后继续执行。当其余代码执行时,echo语句将输出到页面。输出到页面后,您无法使用标题功能重定向。要避免执行其余代码,请使用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.

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

#7


3  

Try adding

ob_start();

at the top of the code i.e. before the include statement.

在代码的顶部,即在include语句之前。

#8


3  

Make Sure that you don't leave a space before <?php when you start <?php tag at the top of the page.

当你在页面顶部启动

#9


2  

Be very careful with whitespace and other stuff that may affect the "output" already done. I certainly know this but still suffered from the same problem. My whole "Admin.php"-file had some spaces after the closing php-tag ?> down the bottom on the last row :)

对空格和其他可能影响已经完成的“输出”的东西要非常小心。我当然知道这一点,但仍然遇到同样的问题。我的整个“Admin.php”文件在关闭php-tag之后有一些空格?>在最后一行的底部:)

Easily discovered by adding...

通过添加...轻松发现

error_reporting(E_ALL);

...which told me which line of code that generated the output.

...告诉我生成输出的代码行。

#10


2  

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(),它们可以停止保存并丢弃保存的内容,也可以分别停止保存并输出所有内容。

#11


2  

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 
}

#12


1  

Try this, Add @ob_start() function in top of the page,

试试这个,在页面顶部添加@ob_start()函数,

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

#13


1  

Use the following code:

使用以下代码:

if(isset($_SERVER['HTTPS']) == 'on')
{

    $self = $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
    ?>

     <script type='text/javascript'>
     window.location.href = 'http://<?php echo $self ?>';
     </script>"
     <?php

     exit();
}
?>

#1


17  

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通过确保逻辑在控制器中并且控制器仅在控制器的末尾触发视图的显示/呈现来在功能上解决它。这意味着您可以决定在操作中的某个位置执行重定向,但不能使用视图。

#2


13  

Use @obstart or try to use Java Script

使用@obstart或尝试使用Java Script

put your obstart(); into your top of the page

把你的障碍();进入页面顶部

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

If you use Javascript Use window.location.href

如果您使用Javascript使用window.location.href

window.location.href example:

 if ((isset($_POST['cancel'])) && ($_POST['cancel'] == 'cancel'))
    {
        echo "<script type='text/javascript'>window.location.href = 'page1.php';</script>"
        exit();
    }

#3


10  

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.

我之前遇到过这种问题,现在我没有使用标题('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']的目的是什么?顺便说一下?如果您想要的只是重定向,只需删除该行。我每次都在使用那个

#4


7  

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

<?php
ob_start();
...
header('Location: page1.php');
...
ob_end_flush();
?>

#5


5  

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.

这肯定会奏效。

#6


3  

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.

佩卡在评论中回答了我的问题。他没有回复,所以我现在。在标题重定向后使用exit()方法。由于某种原因,页面的其余代码在header()方法重定向后继续执行。当其余代码执行时,echo语句将输出到页面。输出到页面后,您无法使用标题功能重定向。要避免执行其余代码,请使用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.

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

#7


3  

Try adding

ob_start();

at the top of the code i.e. before the include statement.

在代码的顶部,即在include语句之前。

#8


3  

Make Sure that you don't leave a space before <?php when you start <?php tag at the top of the page.

当你在页面顶部启动

#9


2  

Be very careful with whitespace and other stuff that may affect the "output" already done. I certainly know this but still suffered from the same problem. My whole "Admin.php"-file had some spaces after the closing php-tag ?> down the bottom on the last row :)

对空格和其他可能影响已经完成的“输出”的东西要非常小心。我当然知道这一点,但仍然遇到同样的问题。我的整个“Admin.php”文件在关闭php-tag之后有一些空格?>在最后一行的底部:)

Easily discovered by adding...

通过添加...轻松发现

error_reporting(E_ALL);

...which told me which line of code that generated the output.

...告诉我生成输出的代码行。

#10


2  

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(),它们可以停止保存并丢弃保存的内容,也可以分别停止保存并输出所有内容。

#11


2  

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 
}

#12


1  

Try this, Add @ob_start() function in top of the page,

试试这个,在页面顶部添加@ob_start()函数,

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

#13


1  

Use the following code:

使用以下代码:

if(isset($_SERVER['HTTPS']) == 'on')
{

    $self = $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
    ?>

     <script type='text/javascript'>
     window.location.href = 'http://<?php echo $self ?>';
     </script>"
     <?php

     exit();
}
?>