TIP: Warning: Cannot modify header information - headers already sent
I thought this would be a useful thread as it seems to be a common error that a lot of new coders don't quite understand.
Many new php coders see this message and will not understand it. In order to understand what PHP is telling us we need to understand how the HTTP process works.
This is a basic web page HTML source code:
When the server sends you this information it sends some information first called reply headers similar to the request headers. These headers are basically text information which the browser does not display but uses internally. After the headers, the server sends a blank line which the browser then understands as the end of the headers. After this blank line is the HTML source for the page.
So what are request and reply headers?
Well for the request they contain many things such as the language the browser accepts, the Browser type, the requested page and sometimes other things. For the reply they contain things such as the content length (so that little blue bar knows when the page is 100% downloaded), the http status code, the server software,the content type, the date and time and some others. You'll see an example further down.
So to recap, a http request and response works like this:
Reply Headers can only be sent ONCE.
This is because the browser has no way of knowing where the html ends (your html may contain many blank lines and the closing </html> tag may also have more content after it).
To understand this better, here is a sample from a http program I have which displays raw output from a server:
To understand how a cookie or session is set, here is an example of the particular header responsible for it which you would see in the reply header:
IMPORTANT NOTE: Output can also be whitespace or a carriage return (End Of Line) character. If you have source code which has either at the beginning of your script this will cause your reply headers to be sent. Here is a sample:
There are 2 main ways to avoid this situation:
1) Restructure your code:
Item 1 can be rather complex and open to debate. The main principle however is that you run any code which triggers a header to be sent BEFORE you print any html. This can actually help you to become a better coder especially if you process all your php first and then merge any variables into a template before printing.
Well thats simple. If you process your php first, you can use session_start(), setcookie() or header() anywhere within your PHP before sending your html output to the clients browser. Additionally it helps you to write clearer code which is easier to understand and modify in the future.
Here is an example of Item 1:
Sometimes however, after you've spent days working on your code it isn't practical to rework all of your code. Fear not my friends, this is where output buffering comes to your rescue.
What is output buffering then?
Output buffering is simply a method PHP uses internally to catch and store all output from your script into its own memory. When output buffering is used, PHP does not pass anything to the server which would trigger the reply headers to be sent. Your script can run as normal, storing output into the output buffer, process php code, perform calculations, database queries, set cookies, start sessions etc and then finally flush the buffer (HTML output) to the browser. This means that using functions such as setcookie() or start_session() will work because the headers have not been sent and thus can be modified internally by PHP and the web server.
Here is an example of the original code (which failed) using output buffering:
I hope that this will help you in your future PHP programming.
|All times are GMT +1. The time now is 06:36 AM.|
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.