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.

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

Warning: Cannot modify header information – headers already sent by (productivity 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 (productivity 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.

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

Delight 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 make pleased. When we try to send a header information after we have already sent some productivity to client, PHP responds by giving this warning.

The most common reasons are

  1. An productivity is sent, any 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 by include()/require() function, and that file may have empty spaces or he lines at the end, that will be sent as productivity.

Vital thing to note is that productivity 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 productivity sent before your call to header function. This force be a right 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 “productivity 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 productivity is coming we can depending on your circumstances we can remove the productivity that is being sent, or if we can’t do that we go the call to header function above that line.

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

Some guideline that force 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 built-in in the other files.

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

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

REFERENCES

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

Check it out:Command Center SkyHi