How can I redirect in PHP without header errors?

前端 未结 7 2122
旧时难觅i
旧时难觅i 2021-02-10 08:04

How can I redirect in PHP with this setup below without getting header output errors, I understand that nothing can be printed to the browser before a header is set, I am lookin

相关标签:
7条回答
  • 2021-02-10 08:11

    As long as you have no script output before the header() function you should be fine. Check there are no echo's or whitespace. Also putting ob_start() at the beginning can help. sometimes there is invisible whitespace - changing the format of your document to ANSI or Unicode may help!

    As a note (although I think you already know) header does not terminate the script so the exit() (which you have) is a definite requirement.

    0 讨论(0)
  • 2021-02-10 08:21

    use { echo '<META HTTP-EQUIV="Refresh" Content="0; URL=process.php">';}

    0 讨论(0)
  • 2021-02-10 08:22

    Use ob_start() in the first line even befor the include. so you can set headers anytime.

    0 讨论(0)
  • 2021-02-10 08:27

    Does the footer.inc.php and SOME-FILE-HERE.php write to the response stream immediately? Because if so, this won't work as you will have already written something before you sent the headers.

    0 讨论(0)
  • 2021-02-10 08:27

    As already mentioned by the others use ob_start() or the output_buffer-setting to buffer the output. Apart from that it's from my point of view not a good practice to output content in the middle of functional code but this is a another topic.

    You can find more information at Google or in this Article about Output Buffering in PHP.

    0 讨论(0)
  • 2021-02-10 08:31

    You need to buffer the ouput so that the HTTP header is not send on the first output. You can either buffer any ouput implicitly by enabling ouput_buffering or explicitly by calling ob_start. But the latter has to be called before the first output, so ideally in the first line of the script that’s initially called.

    0 讨论(0)
提交回复
热议问题