0

I got this warning in php:

Cannot modify header information - headers already sent by (output started at

When I installed mysql, apache, and php separately but when I'm using Wamp the warning does not exist and the page redirects gracefully.

I have already tried setting the error reporting to only display errors and not warnings and what happened is that the warning didn't show up but the page also didn't redirect.

I know this is already considered bad practice but my code base has a lot of these codes and modifying them all might introduce new bugs to the code. Is there any way to completely ignore this warning in php?

Wern Ancheta
  • 22,397
  • 38
  • 100
  • 139
  • 3
    My code is super complicated and contains bugs. Let's not fix the bugs but hide them under the carpet! ... Seriously? There's a reason you're getting this error. Find out why and fix it. – Madara's Ghost Apr 13 '12 at 09:49
  • This is not good practice and I certainly hope that you'll reconsider. Find the errors and fix them. – Leonard Apr 13 '12 at 09:51
  • A super detailed answer is given at https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php/8028987#8028987 – Scott C Wilson Aug 14 '19 at 11:16

5 Answers5

3

The classic way to avoid - and not ignore - this problem is with output buffering, ob_start() and ob_flush(). Start the buffering before any output, and finish with the flush explicitly, or it may do so automatically. This captures any output rather than printing it, and so any headers that are output are done so before any visible content.

Fixing the bug of things being output, from the filename you had in the error message is the best way to fix this though. Removing the closing '?>' from files when it's not needed might help.

Falko
  • 17,076
  • 13
  • 60
  • 105
Alister Bulman
  • 34,482
  • 9
  • 71
  • 110
1

This is an horrible practice but just set

error_reporting(0);

at the beginning of your script. Otherwise setit in php.ini

display_errors = Off

EDIT - probably you are outputting something before the redirect. I once spent three days debugging this and the reason sometimes is whitespace at the end of files or the fact that you output some HTML to the browser before the redirect.
I usually never close the php tag to avoid this

Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
  • will this have a similar effect with setting display_errors = Off in php.ini? If yes then it also won't work, the error won't display but the page isn't also redirected. – Wern Ancheta Apr 13 '12 at 09:48
  • @KyokaSuigetsu probably you are outputting something before the redirect. I once spent three days debugging this and the reason sometimes is whitespace at the end of files: – Nicola Peluchetti Apr 13 '12 at 09:50
  • I also already deleted all the whitespace in the files involved. But I still get this error. I don't know what kind of configuration wampserver did on their php installation, but it just magically works without problems. – Wern Ancheta Apr 13 '12 at 09:52
  • 2
    The wampserver probably has the output buffering set on by default in the php.ini file. – Alister Bulman Apr 13 '12 at 09:55
  • @AlisterBulman: thanks! That solved my problem. I won't use Wampserver ever again. But I'm just concerned about the effect of enabling output buffering. – Wern Ancheta Apr 13 '12 at 10:03
1

Even when you don't show your error your application will not work. This is because like the error said the headers are already sent. What this means is the following: A browser requests your php file at the server. The server then executes this and sends back the resulting html. This error tells you that you are sending something back to the browser after you already sent the result of your php file. So I'd suggest you try to fix the error your getting instead of just not showing it.

Rick Hoving
  • 3,585
  • 3
  • 29
  • 49
1

You can't "ignore" this warning and redirect. The warning says so clearly:

Can't send headers! Headers already send by output!

This means that the header (the Location header cannot be sent because output was already sent. (Headers may only be sent before any and all output).

Please RTM, you need to fix your bugs, not ignore them.


The error is caused by you sending some sort of output before the header() function is called, the reason for that may be a lot of things, when unexpected, the immediate suspect is a whitespace at the beginning or ending of some file.

 <?php //Note the space before the opening tag!
header("Location: this-will-fail.html");
?>
Community
  • 1
  • 1
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
0

Please note that this error specifies exactly where in your code you've started to output data to the browser (see the output started at ...1, look for trailing white space \r, \n, tabs & spaces). In theory, turning off the error reporting will not cause the page to redirect because the headers were already sent and therefore cannot be changed or added to.

What you should do if you need to redirect with data already sent is something like this:

if ( headers_sent() )    // Check if the headers were already sent
{
    echo( "<script>document.location.href='$url';</script>" );  // Use client side redirect
    die();  // Stop here
}
else
{
    header( "Location: $url" );  // Use server side redirect
}

But the best thing would be to find out where the output started and fixing that (if possible).

Yaniro
  • 1,595
  • 9
  • 14