The location of the PHP header works in the middle of the HTML code

I'm having trouble understanding how header("Location: http://www.google.com/"); may work in the middle of the <body> HTML page.
There should be no error, since the header has already been sent due to HTML output before the start of <?php ... ?> .

I am referring to the Cannot modify header information - headers already sent by... warning that I expect to receive.

I am testing this in my local PHP dev environment (Apache / 2.2.15 (Win32) PHP / 5.3.2).

Here is an example:

 <html> <head> </head> <body> <?php header("Location: http://www.google.com/"); ?> </body> </html> 

Any ideas? Thanks.

+4
source share
4 answers

If you have output buffering automatically activated in php.ini, you can emit headers at any time before sending the actual message.

+2
source

This is due to output buffering ...

+7
source

If output buffering is enabled (see ob_start () ), the output will not be sent until the entire page is complete.

+1
source

I noticed this recently when I was doing some kind of dev with the WAMP package on Windows. It caused a lot of hell when I switched to running it on a Linux box. I believe that a configuration value has been configured that causes it to buffer all pages.

-1
source

Source: https://habr.com/ru/post/1312754/


All Articles