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.