How to Fix "Headers Already Sent" Error in Php
When Running My Script, I Am Getting Several Errors Like This: Warning: Cannot Modify Header Information - Headers Already Sent by (Output Started at...
When running my script, I am getting several errors like this:
Warning: Cannot modify header information - headers already sent by (output started at /some/file.php:12) in /some/file.php on line 23
The lines mentioned in the error messages contain header() and setcookie() calls.
What could be the reason for this? And how to fix it?
11 Answers
Must Read
No output before sending headers!
Functions that send/modify HTTP headers must be invoked before any output is made. summary ⇊ Otherwise the call fails:
Warning: Cannot modify header information - headers already sent (output started at script:line)
Some functions modifying the HTTP header are:
Output can be:
Unintentional:
- Whitespace before
<?phpor after?> - The UTF-8 Byte Order Mark specifically
- Previous error messages or notices
- Whitespace before
Intentional:
print,echoand other functions producing output- Raw
<html>sections prior<?phpcode.