Site5 - Built For Designers & Developers MENU
Home  ›  PHP  ›  PHP: Repair “headers already sent” errors

PHP: Repair “headers already sent” errors

11 Comments

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.

Still have a question? Or need help?
If you need technical support with your account, please email us or chat live with a representative.

11 Comments

  • Goodmorning,

    That’s work for me. I linked your website on my blog.

    Thank you

    Bruno Roch

  • there is a porblem header alredy sent .how can i manage it?

  • 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

    • 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.

  • Thank you very much. You have helped me a lot

  • 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

    • My apologies for the confusion. You would simply add the line, as follows:

      output_buffering = On

  • Thank you !

  • Thank you so much, you help me :)

  • Thank you very much, yhis article made my day

Money Back Guarantees