The "Headers Already Sent" error , a brief look at Headers!

Every beginner PHP programmer comes across the “headers already sent” error and then they google to find out how to get rid of it. What they don't try to understand is the root level logic behind that error. Surprisingly alot of junior PHP programmers have no idea what headers are.
I will try to explain in brief what headers are actually. I am not an expert on headers but what i put here is what i know from a little bit of experience.
Every HTTP/HTTPS web page request goes to the relevant web server (Apache , IIS etc ) and then the web server responds with the requested page. COOL!!! BUT how do the web server know what page is requested and what form data has been posted etc? HEADERS!
Headers are the means of communication between a browser and a web server. A normal web request from a browser for http://www.abc.com/index.php would look something like this in headers.
GET /index.php HTTP/1.1Host: www.abc.comConnection: CloseThe first line tells the web server that what transmission method is used GET or POST. Then the page name wanted and the protocol version used.The second line tells the web server what domain that page should be under, this way apache can look inside its virtual hosts list and see what domain resides in which directory. So if www.abc.com resides in /home/abc/public_html , then the page requested becomes /home/abc/public_html/index.php.
The third line tells the server that after the request is processed the connection will be closed.
So that was a simple request. Now its the servers turn to respond, in our case the page is a .php page , so the php engine will process the requested page and send the response. Before any of the html is sent back to the browser , PHP sends in some information about the server and the data coming going through. Here is a sample response header.
HTTP/1.0 200 OKDate: Sun, 25 Feb 2007 21:28:38 GMTServer: Microsoft-IIS/6.0Content-Type: text/htmlContent-Length: 4293Connection: close
First line gives the HTTP status code of the response. “200 OK” in this case. Which means all good , i found the requested page and i have processed it for you and i am going to send it through. A list of status codes can be found http://en.wikipedia.org/wiki/List_of_HTTP_status_codes. Next line is ofcourse the date and time of the server, then comes the name of the web server in use. Next line is content type , which you are probably already familiar with. Content type tells the browser what sort of data is going to come through , so that the browser can show it accordingly. In this case it is text/html , which the browser will show happily. Next comes the content length , ofcourse this is byte size of the data that is going to flow through. Last line as before tells the browser the connection will be closed once the data goes through.
Once the response header is complete , the php script output follows. So important thing to remember header first and then the data from your script.The PHP header() function basically you can add to the above header response from the server.For instance when you type in your php script..
header(”Location:login.php”);
You are basically adding a line to the above response header..
Location:login.php
Which means that please goto login.php for further processing and the browser obliges and heads to login.php.
When your php script even sends one ” ” (space) basically the data has started to flow to the users browser, as we know before any data flow the header goes out first. Hence that means the header has been sent to the browser, followed by your ” “(space) character. Now once the header has been sent and PHP sees some command like header(”Location:goto.php”) , PHP says … excuse me sir but you have already started to send output from your script , thus i have already sent out the response headers , now how do you expect me to add that line to the response header ?
To overcome this problem alot of programmers use ob_start() at the top , basically telling PHP to buffer the output until the end of the script or flushed explicitly with ob_end_flush or ob_flush. So basically no output is actually sent to the browser and the header command works fine. Now gentlemen that works fine and its a wonderful utility to have at your disposal BUT ….. if you are using ob_start() to get rid of header already sent errors , then you have a flow problem in your script. Fix the flow instead of just using ob_start as a quick band aid , you wont become a good programmer and last in the long run relying on shortcuts!
The ob_start function is best left to greater uses like using it for compression or using it with a callback function to process your output before sending out the script output or various other things.
Hope this article helps someone out there!

COUNTER