PHP Warning: Cannot modify header information – headers already sent – It is caused due to Functions that send/modify HTTP headers must be invoked before any output is made. Otherwise the call fails:
Warning: Cannot modify header information – headers already sent (output started at script:Line Number)
HTTP HEADERS are very sensitive. The function calling header function should be called before any output such as echo or html page embedded in PHP Script.
The functions which can modify the http header:
Sometimes, intentional mistakes can also cause the error:
Some other times, unintentional mistakes can also cause the error:
PHP scripts mainly generate HTML content, but also pass a set of HTTP/CGI headers to the webserver:
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
If php script encounters print or echo function in the php script, then first of all it flushes our all headers. Therefore it fails to send the http header.
Warning message contains enough information about the error.
Warning: Cannot modify header information – headers already sent by (output started at /username/htdocs/process.php:165) in /username/htdocs/datadisplay.php on line 365
The above code tells us that at the line 365 of datadisplay.php script, it failed to invoke header().
Further, the error message also says, output started at…., so our target line code to look into is process.php and code line number 165.
Is there a way to check headers are sent successfully?
Yes. PHP is providing one function header_sent. Using this function, we can check whether the header are sent successfully.
die(“Headers already sent”);