Friday 28 June 2013

Resolving "headers already send" warning in PHP

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.
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:wampwwwaktesterror.php:2) in C:wampwwwaktesterror.php on line 4

Warning: session_start() [function.session-start]: Cannot send session cookie – headers already sent by (output started at C:wampwwwaktesterror.php:2) in C:wampwwwaktesterror.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:file.php");
?>

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 :-


  • 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).
  • 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.

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

Some guideline that might help


  • Always start “<?php” at the first line and first column, in you php file.
  • Keep all you your session related function like session_start() at the start of file, just after “<?php”
  • 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.
  •  use ob_start(); at begining of php file. This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.The contents of this internal buffer may be copied into a string variable using ob_get_contents(). To output what is stored in the internal buffer, use ob_end_flush(). Alternatively, ob_end_clean() will silently discard the buffer contents.


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

No comments:

Post a Comment