开发者

php headers_sent function is not working

<h1>Header</h1>
<?php
echo 'teste'; // output here
echo headers_sent(); // no output here!
?>

Why heade开发者_开发技巧rs_sent() doesn't output in this case? Thank you.


Look at your php.ini config file and look for line containing output_buffering and make sure it looks like:

output_buffering = Off

If you have it Off then echo headers_sent() should output 1

If you have it On then echo headers_sent() won't output anything because headers_sent() in that case will return FALSE, because headers (meaning not HTML <h1> but HTTP response headers) were not sent yet because output is buffered.

To force sending headers and output echo-ed so far you may call flush()


Because it returns true or false.

var_dump(headers_sent());

Should display (one or the other below)

bool(true)
bool(false)

It is working, it just will not output text, if it is false, as it is not text it is a boolean value. The general use of this function is meant for an if statement, not a displaying statement, if you want to display it simply use the ternary operator

echo (headers_sent())?'true':'false';

Edit

Thanks to Victor for correcting me: It will return 1 if true, an empty string if false.

Update

Why does headers_sent() return false? Well to clarify I will post from the manual:

headers_sent — Checks if or where headers have been sent

Basically, when you have any output, that sends the headers automatically to the browser and starts the body. For example:

<?php
echo "test";
echo headers_sent(); // should yield 1
?>

That should display 1, since we had an echo statement for the headers_sent call.

<?php
echo headers_sent(); // should yield empty string
?>

That will display an empty string, given that no output is before the headers_sent() call. The above assumes that output_buffering is turned off. As with output_buffering being on it saves all output till the script is done processing then displays that output, thus the header / body tag get sent at the end of the script.

Hopefully that clears it up. If not, see the manual link I posted above and read through the examples at the manual.


This could happen if ob_start has been called before the code you showed has been executed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜