开发者

error during page redirection [duplicate]

开发者 https://www.devze.com 2023-01-02 05:54 出处:网络
This question already has answers here: How to fix "Headers already sent" error in PHP (11 answers)
This question already has answers here: How to fix "Headers already sent" error in PHP (11 answers) Closed 9 years ago.

when I redirect my page to another page header("location:popup.php"); it gives following error -

Cannot modify header information - headers already sent by 开发者_如何学Python(output started at C:\xampp\htdocs\303\levelupdate.php:289) in C:\xampp\htdocs\303\levelupdate.php on line 320


You can only do redirect if nothing has been sent to the browser already. This means you have to do one of two things:

  1. Work out if you need to redirect before you send any data.

  2. Use ob_start to hold the page until it has finished processing. As soon as you throw a header call, it will dump the buffered copy and just send the header. But you have to call ob_start right at the beginning so it catches any output. Its PHP page should give you some clues on this.

My personal preference lies with output buffer (ob) but it will mean no data gets sent to the client until the whole page is done rendering. This can sometimes make the site seem a little slow if the page takes a long time to generate.

You can combat slowness by calling ob_flush which sends the contents of the buffer to the client. I recommend doing this as soon as you know you won't need to send a header change. But it's completely optional and not worth the effort for simple and quick scripts.


This error means some body text has been sent to the browser. This isn't allowed, because headers always go first. Maybe there is whitespace, or have you echo'd HTML?

The output might also be a previous error. header('location: ...') requires a fully-qualified URL (i.e. with http://domain.com).


the header command only works if no output has been sent to the page. Therefore you need to ensure that no html or any other output like php echos has been sent to the page before being called.

Check if you have empty rows on the top of your file before your php declaration.


it means that you have already output some content (i.e. echo) before doing header("location:popup.php") and therefore it won't work.


You need to ensure that nothing is written after a header declaration.

Detailed explanation

To fix, put exit; under your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号