PHP: Repair “headers already sent” errors
How To Repair “headers already sent” PHP Errors
Sometimes you my see an issue loading your site ( sometimes a 500 error ) and when you look into the Apache logs, you see a similar message:
PHP Warning: Cannot modify header information – headers already sent by (output started at /home/someuser/public_html/somescript.php)
This normally can be resolved by a quick custom .user.ini alteration:
1) Create a custom .user.ini file (if there isn’t one already)
For information on how to create a custom .user.ini file, please click here.
2) Locate the line that looks like:
output_buffering = Off
3) Change the output_buffering line to:
output_buffering = On
This should be all that is needed to bring the site back online.
This normally fixes issues when trying to do redirects using php. Below we explain how changing this helps.
Doing a header redirect requires outputting an HTTP header with the response. The problem is, if PHP has already begun outputting the body of the document, it cannot then output a header because the headers ended when the body started.
Output buffering prevents PHP from outputting any part of the body of the document until the output buffer is flushed (or PHP exits). This allows you to output headers at any time.
Bruno Roch March 10, 2014 at 9:30 am
Goodmorning,
That’s work for me. I linked your website on my blog.
Thank you
Bruno Roch
sibaram sahu April 25, 2014 at 8:51 am
there is a porblem header alredy sent .how can i manage it?
James Davey April 25, 2014 at 9:07 am
Hello Sibrahim,
The steps laid out in this article should help you with this, but you can try a different tack as well. Using the article at http://kb.site5.com/php/how-to-make-custom-php-changes-using-a-user-ini-file/ simply add this line:
output_buffering = On
That should take care of this for you.
Saber ahmadi July 13, 2014 at 8:43 am
Hi everybody
also you can use this code directly in your php code.
// Creating an uneraseable output buffer in a way compatible with both PHP 5.3 and 5.4
if (version_compare(PHP_VERSION, ‘5.4.0’, ‘>=’))
{
ob_start(null, 0, PHP_OUTPUT_HANDLER_STDFLAGS ^ PHP_OUTPUT_HANDLER_REMOVABLE);
}
else
{
ob_start(null, 0, false);
}
cheers,
http://www.rayantabib.com
James Davey July 14, 2014 at 7:37 am
Hi Saber,
Thank you for your input. I cannot state for certain that this should be used (though it appears like it would work normally), as I am sure you understand, and would clarify that you should always test code provided like this in a secure environment.
Athul K Vijayan February 25, 2015 at 1:40 am
Thank you very much. You have helped me a lot
mobby March 3, 2015 at 7:04 am
Hi, This not worked for me . Please note that the steps you mentioned are
1) Create a file named .user.ini
2) Locate line ?
how can I locate a line in an empty file I just created ?
Please correct me if I am confused or the steps are not mentioned right.
Thank you
James Davey March 3, 2015 at 1:34 pm
My apologies for the confusion. You would simply add the line, as follows:
output_buffering = On
mobby March 3, 2015 at 2:12 pm
Thank you !
Hay Marn March 7, 2015 at 6:22 am
Thank you so much, you help me :)
Jackson July 9, 2015 at 3:41 am
Thank you very much, yhis article made my day