Redirect HTTP header after output

While working on my current project, I found a little curiosity. if I try to redirect the URL AFTER any output, it still works fine. but according to php manual it should not work.

error_reporting(-1); echo "test"; header("Location: http://www.google.com/"); 

redirect me to google without any errors. what's wrong with that (mybe my apache config? dunno ...). it also works with cookies, etc.

I use IE9 (tested it also with opera) and Apache / 2.2.21.

thinks alex

0
source share
3 answers

Thanks to the messages of Will and pieman, it turned out that there is an output buffer std, which I can not disable or prejudice. to fix this "problem" I posted

 ob_end_clean(); 

in the first line of my index.php. the reason is that my source test code is not working (what I expect). it really is not nice, but necessary. if any of you can provide a β€œbetter” solution, I would be grateful.

thinks alex

+1
source

When creating comments by Will and Ganesha, you can also try ob_get_status() to see if you have any output buffers opened during the echo call. If you do this, your echo will be flushed to the buffer and will not be immediately sent to the client. Then header() can be called without error, because the content has not been sent.

0
source

Because of the PHP INI settings, your data is buffered before being output. Thats why its successfully redirecting to the next page

-1
source

All Articles