PHP Tips – Warning: Cannot modify header information. Let’s get things straight
Tell me the truth: you have had to mix it up with this error at least once, haven’t you?
In this pill we’ll get things straight, once and for all, how this error occurs, how to avoid it and, if it’s not possible to avoid it, how to eventually bypass it.
When does this error come up?
Some PHP functions add some information to the headers which the web server sends to the browser when a page is requested. The main functions of this kind are:
As soon as we have an output in a document (any output), the web server starts sending the headers to the client. This, for example, is the header that Your Inspiration Web sends me
Date: Mon, 01 Nov 2010 13:55:15 GMT
Server: Apache/2.2.15 (Unix) mod_ssl/2.2.15 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/184.108.40.20635
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
WP-Super-Cache: Served legacy cache file
Keep-Alive: timeout=5, max=100
Content-Type: text/html; charset=UTF-8
If I set a cookie, a row like this would be added
Set-Cookie: name=value; expires=timestamp of cookie expiration
Now, if we write
the web server will release the headers as soon as it meets this instruction.
At this point having done that, if I wanted for example to set a cookie, I would no longer be able to. In fact, the cookie data are sent to the request header, but the header has already been launched and thus the infamous error will come up.
I repeat: any output
This script also will lead to the notorious error
<?php // notice that there's a “new paragraph ” before the opening of the php tag session_start(); //....
In fact before the opening of the PHP tag we are in HTML mode, and in this modality a “new paragraph” is, whether you want it or not, an output.
By the way, often the error is exactly this (and maybe it’s an error contained in some file comprised and that makes us lose a lot of precious time)!
The solution is obviously to use functions that modify the header at the beginning of the script, or however, before any output. This can be done in 99,5% of the cases, even when it might look impossible, and it’s the best solution.
However, in rare circumstances, this procedure might not be possible.
In this case we could use the ob_start() function (to be invoked at the beginning of the script). This function keeps the output in buffer until the end of the execution of the script.
Therefore this script will not lead to errors:
<?php ob_start(); ?> <h1>Hello</h1> <?php session_start(); ?>
This solution is to be used with thrift because the storage of outputs on the server will have a burden on the performance. We can partially overcome this problem liberating the buffer once there is no longer need for it through the ob_end_flush() function.
<?php ob_start(); ?> <h1>hello</h1> <?php session_start(); ob_end_flush(); //other code and other content.... ?>
In this pill we dealt with what is considered a headache for many, in reality it’s just a trivial thing which can be resolved with a minimal effort.
And you? Have you ever got stuck with this error?
L'immagine principale dell'articolo è stata fornita da @Fotolia