How to fix “Headers already sent” error in PHP
Accidental whitespace, often at the beginning or end of files, like this:
<?php // Note the space before "<?php" ?>
To avoid this, simply leave out the closing
?> - it's not required anyways.
- Byte order marks at the beginning of a php file. Examine 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 BFfrom the start of files.
- Explicit output, such as calls to
passthru, code before
- A warning outputted by php, if the
display_errorsphp.ini property is set. Instead of crashing on a programmer mistake, php silently fixes the error and emits a warning. While you can modify the
display_errorsor error_reporting configurations, you should rather fix the problem.
Common reasons are accesses to undefined elements of an array (such as
issetto test whether the input is set), or using an undefined constant instead of a string literal (as in
$_POST[input], note the missing quotes).
However, while output buffering avoids the issues, you should really determine why your application outputs an HTTP body before the HTTP header. That'd be like taking a phone call and discussing your day and the weather before telling the caller that he's got the wrong number.
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
What could be the reason for this? And how to fix it?
Instead of the below line
echo("<script>location.href = '".ADMIN_URL."/index.php?msg=$msg';</script>");
?><script><?php echo("location.href = '".ADMIN_URL."/index.php?msg=$msg';");?></script><?php
It'll definitely solve your problem. I faced the same problem but I solved through writing header location in the above way.
Another bad practice can invoke this problem which is not stated yet.
See this code snippet:
<?php include('a_important_file.php'); //really really really bad practise header("Location:A location"); ?>
Things are okay,right?
What if "a_important_file.php" is this:
<?php //some php code //another line of php code //no line above is generating any output ?> ----------This is the end of the an_important_file-------------------
This will not work? Why?Because already a new line is generated.
Now,though this is not a common scenario what if you are using a MVC framework which loads a lots of file before handover things to your controller? This is not an uncommon scenario. Be prepare for this.
From PSR-2 2.2 :
- All PHP files MUST use the
Unix LF (linefeed) line ending.
- All PHP files MUST end with a
single blank line.
- The closing ?> tag MUST be
omittedfrom files containing
Believe me , following thse standards can save you a hell lot of hours from your life :)
Sometimes when the dev process has both WIN work stations and LINUX systems (hosting) and in the code you do not see any output before the related line, it could be the formatting of the file and the lack of Unix LF (linefeed) line ending.
What we usually do in order to quickly fix this, is rename the file and on the LINUX system create a new file instead of the renamed one, and then copy the content into that. Many times this solve the issue as some of the files that were created in WIN once moved to the hosting cause this issue.
This fix is an easy fix for sites we manage by FTP and sometimes can save our new team members some time.
It is because of this line:
printf ("Hi %s,</br />", $name);
You should not print/echo anything before sending the headers.