views:

88

answers:

4

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.

A: 

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

$old_er = error_reporting(0);

header(...)

error_reporting($old_er);

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

strager
+6  A: 

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

Peter Smit
I forgot that all headers are sent at once. You're right.
Alan
+1  A: 

headers_sent()

soulmerge
+1  A: 
if (headers_sent())
{
    trigger_error("Here you got your warning", E_WARNING);
}
sshow