简体   繁体   中英

'php headers already sent' no longer an issue?

In the php manual ( http://php.net/manual/en/function.header.php ) I find the following about http headers:

QUOTE Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

<html>
<?php
/* This will give an error. Note the output
 * above, which is before the header() call */
header('Location: http://www.example.com/');
exit;
?>

ENDQUOTE

I tried this now (making a study of the HTTP protocol) and find that this doesn't seem to be an issue anymore; I wonder what has changed: has the way php deals with parsing webpages changed? I tried several browsers (Chrome, IE10, IE7) and OS (Windows 8.1, XP) and none of them seem to have an issue with this anymore (see informaticaschool.net76.net/httpdemo3.php for a page that includes exactly the above code example, not giving an error.

For the details of the php version running on that server (I also tried a different apache server) see informaticaschool.net76.net/phpinfo.php.

I admit its a bit of a strange thing to ask why an error does NOT occur but as pointed out i'm studying the workings of http. Looking into a Wireshark trace now to find out what happens exactly in this case, that I knew to be an issue before, I am now surprised to find that NO error appears (and not finding an explanation for it in the php manual).

That server has output_buffering turned on, so all output is buffered and held, which allows you to send headers to the web server even after you have already produced output. See the canonical answer about this issue , section "But it worked on the other server!?" .

You must send headers before send output buffer.

Or you can use functions ob_start and ob_end_flush to save output buffer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM