Headers already sent
When you running the php code or some script you will get this error on screen
Warning: Cannot modify header information - headers already sent by (output started at /some/file.php:12) in /some/file.php on line 36
You will seen this error lot of times in your development,Even sure you will seen this once in your web development.
Reasons And Solutions:–
This error message is occur when anything is sent before you send HTTP headers.SO no output before sending the header.Functions that send/modify HTTP headers must be invoked before any output is made.If not the call fails and send error like
Warning: Cannot modify header information - headers already sent (output started at file:line)
White spaces before <?php tag
This error comes because you leaves white spaces before the
?> <?php //you can see spaces before the <?php tag
Byte Order Mark(BOM) at page start
Byte order mark at the starting of the page.These are nomally not shown in most of the text editors.Check your php files with a hex editor to find out whether that’s the case. They should start with the bytes
3F 3C. You can safely remove the BOM
EF BB BF from the start of files.
White spaces after ?> tag
Some time we leave white spaces etc after the closing php tag ?> . Any charcater after closing tag means the content are still occur in page and produced the error.So to avoid it less use the ?> tag .
Error messages or notices
A warning outputted by php, if the display_errors php.ini property is set. Instead of crashing on a programmer mistake, php silently fixes the error and emits a warning.So set the display_error in your php.ini file on.If the display_error are disable in you php.ini then use these ones to display the error and cause of the error.
error_reporting(E_ALL); ini_set("display_errors", 1);
Using print() or echo before calling header() or session_start().Do not or never echo or print before the session_start() or any header call .For testing purpose we use the echo and print in our files and forgot to remove them.These can cause the header already sent error.
Unparsed HTML sections in a .php file are direct output as well. Script conditions that will trigger a header() call must be noted before any raw
<!DOCTYPE html> <?php // headers already sent.
Output Buffering Solve The Problem
Turning on the output buffering will solve the problem.Use the ob_start() at the start of the php code
<?php ob_start(); // code ob_end_flush(); ?>
All output after the start of the ob_start() is buffered in memory until you release the buffer for example with ob_end_flush().But output buffering will ovoid this issue.