warning cannot - 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.
It is because of this line:
printf ("Hi %s,</br />", $name);
You should not print/echo anything before sending the headers.
(copied from: source)
1) there should not be any output (i.e.
echo.. or HTML codes) before the
2) remove any white-space(or newline) before
<?php and after
3) GOLDEN RULE! - check if that php file (and also, if you
include other files) have UTF8 without BOM encoding (and not just UTF-8). That is problem in many cases (because UTF8 encoded file has something special character in the start of php file, which your text-editor doesnt show)!!!!!!!!!!!
header(...); you must use
5) always use 301 or 302 reference:
header("location: http://example.com", true, 301 ); exit;
6) Turn on error reporting. And tell the error.
7) If none of above helps, use JAVSCRIPT redirection(however, strongly non-recommended method), may be the last chance in custom cases...:
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.