Skip to content Skip to sidebar Skip to footer

Headers_sent() Return False, But Headers Were Sent

My code is simple: ... It returns false. Shouldn't the headers be sent immediate

Solution 1:

It depends if your output_buffering directive in php.ini file. If it is Off

output_buffering = Off

then echo headers_sent() should output 1

In other cases, headers_sent() won't output any results because it will be FALSE. The headers won't be sent because the output is buffered.

If you want to get around this and force-send headers, you can use flush().

Hope this helps!

Solution 2:

Read the comments in docs!

Here, for example: http://es1.php.net/manual/en/function.headers-sent.php#75835

He do a great exposition :P

Edit

Yes, headers_sent() will return false, even if you sent something to the ouptut using print() or header() , if output_buffering is different from Off in you php.ini, and the length of what you sent does not exceed the size of output_buffering. [...] This is noticed in php.ini comment : "Output buffering allows you to send header lines (including cookies) even after you send body content, in the price of slowing PHP's output layer a bit."

Solution 3:

I managed to find a way without deactivating the output_buffering :

if (!headers_sent() && !ob_get_contents()) {
    // do your thing
}

Post a Comment for "Headers_sent() Return False, But Headers Were Sent"