Friday, April 29, 2011

Check whether content already sent in PHP

Is there a smart way to check whether output has already been sent to the browser and sending a header would generate a PHP_WARNING?

Obviously there's the alternative of using an output buffer, but that's not always an option.

From stackoverflow
  • If all you want is to hide the warning, just turn off error reporting:

    $old_er = error_reporting(0);

    Or, you can redirect PHP errors and warnings to a log file (which is preferable in production, IMO).

  • You can use the headers_sent() method. This because before anything is outputted, the headers will be send first.

    Alan : I forgot that all headers are sent at once. You're right.
  • headers_sent()

  • if (headers_sent())
        trigger_error("Here you got your warning", E_WARNING);


Post a Comment