Home  >  Article  >  Backend Development  >  header已经发送,为何还是返回false

header已经发送,为何还是返回false

WBOY
WBOYOriginal
2016-06-13 11:55:421341browse

header已经发送,为什么还是返回false
header("Content-type: text/html; charset=gbk");       //发送一个报头
$a=headers_sent();
var_dump($a);           //false

这里为什么返回false?????
------解决方案--------------------
手册中有人提到这个问题:
http://cn2.php.net/manual/en/function.headers-sent.php#86488
------解决方案--------------------

header("Content-type: text/html; charset=gbk");  
flush();//刷新输出缓冲
var_dump(headers_sent());

------解决方案--------------------
bool headers_sent ( [string &file [, int &line]] )

如果 HTTP 标头尚未被发送出去的话,headers_sent() 将返回 FALSE,否则返回 TRUE。如果指定了可选参数 file 和 line, headers_sent() 将会把 PHP 的文件名以及从哪一行开始有输出放到 file 和 line 变量中。 

一旦标头已经被发送,将不能再使用 header() 函数来发送其它的标头。使用此函数至少可以避免避免与 HTTP 标头有关的错误信息。另一个选择是使用输出缓存。 

php 默认输出缓存是打开的,所以 headers_sent 总是返回假
#3 的 flush() 将输出缓存中的内容推出(真正输出了),所以其后的 headers_sent 返回真

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn