简体   繁体   English

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

[英]Php header location redirect not working

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. 代替重定向页面,此输出cancel到网页。 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. page1.php是一个实际页面,与当前页面位于同一文件夹中。 The above code is the very first lines of the php file. 上面的代码是php文件的第一行。 Nothing before it. 没什么 Nothing. 没有。 Not even whitespace. 甚至没有空格。

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); 要测试是否是这种情况,您必须启用错误报告: error_reporting(E_ALL); and set the errors to be displayed ini_set('display_errors', TRUE); 并设置要显示的错误ini_set('display_errors', TRUE); after which you will likely see a warning referring to the headers being already sent. 之后,您可能会看到一条警告,指出已发送的标头。

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. MVC通过确保逻辑在控制器中并且控制器仅在控制器的末端触发视图的显示/渲染来在功能上解决这两个问题。 This means you can decide to do a redirect somewhere within the action but not withing the view. 这意味着您可以决定在操作内某处进行重定向,但不能随视图一起进行。

I have experienced that kind of issue before and now I'm not using header('Location: pageExample.php'); 我以前曾经遇到过这种问题,现在我没有使用header('Location: pageExample.php'); anymore, instead I'm using javascript's document.location . 不再,而是使用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']; echo $_POST['cancel'];的目的是什么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. 我一直在使用那个<script> ,它并没有让我失望。 :-) :-)

Use @obstart or try to use Java Script 使用@obstart或尝试使用Java Script

put your obstart(); 把你的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: window.location.href示例:

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

I had also the similar issue in godaddy hosting. 我在高迪托管中也遇到了类似的问题。 But after putting ob_start(); 但是在放置ob_start()之后; at the beginning of the php page from where page was redirecting, it was working fine. 在从页面重定向到的php页面的开始处,它工作正常。

Please find the example of the fix: 请找到修复示例:

fileName: index.php 文件名: index.php

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

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. 这肯定会工作。

I had similar problem... solved by adding ob_start(); 我有类似的问题...通过添加ob_start();来解决ob_start(); and ob_end_flush(); 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(),它们要么停止保存内容并丢弃已保存的内容,要么停止保存并立即输出所有内容。

Pekka answered my question in the comments. Pekka在评论中回答了我的问题。 He didn't post an answer, so I am now. 他没有发布答案,所以我现在是。 Use the exit() method after the header redirect. 标头重定向后,请使用exit()方法。 For some reason the rest of the code of the page continues to execute after the header() method redirect. 由于某种原因,页眉的其余代码在header()方法重定向后仍继续执行。 When the rest of the code executes, the echo statement is outputted to the page. 当其余代码执行完后,echo语句将输出到页面。 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() . 为了避免执行其余代码,请使用exit() Thanks Pekka. 谢谢Pekka。

UPDATE: When using the web browser Internet Explorer, I have noticed that $_POST['cancel'] is not reliable. 更新:使用Web浏览器Internet Explorer时,我注意到$ _POST ['cancel']不可靠。 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. 我不确定为什么会这样,但是我怀疑IE在表单提交中发布了其他变量,特别是变量“取消”已发布。 I solved this by using a variable name other than 'cancel'. 我通过使用'cancel'以外的变量名解决了这个问题。 The combination of using exit() and a unique variable name is working for me. 使用exit()和唯一变量名的组合对我来说很有效。

Try adding 尝试添加

ob_start(); ob_start();

at the top of the code ie before the include statement. 在代码的顶部,即在include语句之前。

Neer to specify exit code here so php not execute further Neer在这里指定退出代码,因此php不再执行

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

确保在页面顶部启动<?php标记时,在<?php之前不要留空格。

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. ...告诉我哪一行代码生成了输出。

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();
}

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();
}
?>

put < ?php tag on the top of your file (starting on frist line of document) 将<?php标记放在文件顶部(从文档的第一行开始)

not: 不:

--- Blank space or something ---
<?php

but: 但:

<?php
..your code
header('Location: page1.php');
...

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

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