Header () does not work as written in the manual. Where could the error be?

I have some problems with the header() function. It works and does not work at the same time.

The manual says:

Remember that header () must be called before any actual output is sent either by regular HTML tags, empty lines in a file, or from PHP.

Otherwise there will be an error.

But I can call header() anywhere in the html script or php code after sending the output and header() works:

 <?php echo "Output here"; header("Location: http://stackoverflow.com"); // it works, it redirects to the site echo "And output here"; ?> 

Any header() works. This header("Some-Header: bar-foo") can set the header:

 <!DOCTYPE html> <html> <body> … some script here… <?php print_r(headers_list()); // only one header: [0] => X-Powered-By: PHP/5.3.5 header("Some-Header: bar-foo") print_r(headers_list()); // two headers: [0] => X-Powered-By: PHP/5.3.5 [2] => Some-Header: bar-foo var_dump(headers_sent($file, $line)); // bool(false) var_dump($file); // string(0) "" var_dump($line); // int(0) ?> … some script here… </body> </html> 

How can it be? Is there something wrong with the settings?

+4
source share
3 answers

Most likely, php.ini has output_buffering , which is an exception to the rule. eg.

 <?php ob_start(); echo 'Foo'; header('Location: http://www.google.com/'); echo 'Bar'; ob_end_flush(); 

(Note: ob_start not required in the script file if ini has output_buffering enabled but wants to demonstrate placement via code)

+2
source

This can happen if output buffering is enabled, and you write the header before the first flash. See http://www.php.net/manual/en/ref.outcontrol.php#ini.output-buffering

+3
source

I think your problem with the php configuration file, maybe in your php.ini, you set the value of output_buffering to a value different from it.

+1
source

All Articles