error during page redirection [duplicate]
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:
Work out if you need to redirect before you send any data.
Use
ob_start
to hold the page until it has finished processing. As soon as you throw aheader
call, it will dump the buffered copy and just send the header. But you have to callob_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.
精彩评论