Headers_sent () returns false, but headers sent

My code is simple:

<!DOCTYPE html> <html> <head> ... <?php var_dump(headers_sent()); ?> 

It returns false. Should headers be sent immediately after printing? As immediately after the first character < .

+4
source share
3 answers

It depends on your output_buffering directive in the php.ini . If it's Off

output_buffering = Off

then echo headers_sent() should output 1

In other cases, headers_sent() will not output any results, because it will be FALSE. Headers will not be sent because the output is buffered.

If you want to get around this and force the sending of headers, you can use flush() .

Hope this helps!

+4
source

Read the comments in the docs!

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

He makes a big exposure: P

Edit

Yes, headers_sent () will return false even if you sent something to Ouptut using print () or header (), if output_buffering is different from Off in php.ini, and the length you send does not exceed the size of output_buffering, [... ] This is noted in the php.ini comment: "Output buffering allows you to send header lines (including cookies) even after sending the contents of the body, in the price slightly slowing down the PHP output level.

+2
source

I managed to find a way without deactivating output_buffering:

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

Source: https://habr.com/ru/post/1490857/


All Articles