Tuesday, July 20, 2010

Warning: headers already sent

SkyHi @ Tuesday, July 20, 2010

When I was learning PHP their were few things that had stumped me, I would usually take it hours to solve them if not days. Now I have grown up, and I realize that those were the basic things that should have been mentioned in the PHP manual, but they were not.


Today I am going to write about one such annoying problem that almost always comes in the way of PHP developers at least once.


Warning: Cannot modify header information – headers already sent by (output started at C:\wamp\www\aktest\error.php:2) in C:\wamp\www\aktest\error.php on line 4


Warning: session_start() [function.session-start]: Cannot send session cookie – headers already sent by (output started at C:\wamp\www\aktest\error.php:2) in C:\wamp\www\aktest\error.php on line 3


This is “header already sent” warning message, that we get whenever we try to redirect a user to some other page or location, when we try to set a cookie or if we try to start a session. Once we get this warning we know that page won’t redirect, session won’t start or cookie’s sent to user, So we can’t even ignore them.


Before we see the solutions to resolve this warning let’s see the source code of the error.php file that I used to generate this error.


<?php
session_start();
header("location:http://localhost/");
?>

Please notice an empty line before “<?php”, this is the cause of errors in his page.



Why This Occurs


This is requirement of the http protocol that header related information must be sent by the server before it can sent the content. When we try to send a header information after we have already sent some output to client, PHP responds by giving this warning.


The most common reasons are


  1. An output is sent, either by normal HTML tags, blank lines in a file, or from PHP file itself(in my case this is the reason).
  2. We read a file using include()/require() function, and that file may have empty spaces or he lines at the end, that will be sent as output.

Important thing to note is that output is sent before header information that you wanted to sent, and hence the warning.


How To Resolve


This is really simple, just make sure their is no output sent before your call to header function. This might be a correct advice but this still does not help much.


What really needed is that we need to know where is the exact problem, that we can resolve.


So let’s take a look at the warning message again, now look at the part where in parenthesees it says “output started at <path of the file>: line number“, you see the source of our problem is already mentioned in this warning. This is where the problem lies.


Now we know from where output is coming we can depending on your situation we can remove the output that is being sent, or if we can’t do that we move the call to header function above that line.


Basically we have to make sure that no output is sent before call to any header related functions.


Some guideline that might help


  1. Always start “<?php” at the first line and first column, in you php file.
  2. Keep all you your session related function like session_start() at the start of file, just after “<?php”
  3. If all you are writing is a php file then do not use “?>”, this can prevent empty space or lines from being included in the other files.

It would be interesting to know what you did when you first saw this problem, what was your reactions.


If you have any doubts or you did not understand something leave a comment below, I will try to help you as time permits.


REFERENCES

http://amiworks.co.in/talk/warning-headers-already-sent/