Home  >  Article  >  Backend Development  >  php彻底根绝warning: Cannot add header information - headers already sent in.

php彻底根绝warning: Cannot add header information - headers already sent in.

WBOY
WBOYOriginal
2016-06-13 11:02:36862browse

php彻底杜绝warning: Cannot add header information - headers already sent in......

解决方案:

1)适用于有权限编辑PHP.INI的人

打开php.ini文件(你应试比我清楚你的php.ini在哪里),找到

output_buffering =改为on或者任何数字.如果是IIS6,请一定改为ON,不然你的PHP效率会奇慢.

2)使用虚拟主机,不能编辑PHP.INI,怎么办?

简单:

在你的空间根目录下建立一个.htaccess文件,内容如下:

AllowOverride All
PHP_FLAG output_buffering On

不幸的情况是:还是不行?全部网页都不能显示啦?

那么,你可以打电话骂一通空间商,然后让他给你把apache的.htaccess AllowOverride打开

3)在PHP文件里解决

ob_start()
启用output buffering机制。 Output buffering支持多层次 -- 例如,可以多次调用 ob_start() 函数。

ob_end_flush()
发送output buffer(输出缓冲)并禁用output buffering机制。

ob_end_clean()
清除output buffer但不发送,并禁用output buffering。

ob_get_contents()
将当前的output buffer返回成一个字符串。允许你处理脚本发出的任何输出。

原理:

output_buffering被启用时,在脚本发送输出时,PHP并不发送HTTP header。相反,它将此输出通过管道(pipe)输入到动态增加的缓存中(只能在PHP 4.0中使用,它具有中央化的输出机制)。你仍然可以修改/添加header,或者设置cookie,因为header实际上并没有发送。当全部脚本终止时,PHP将自动发送HTTP header到浏览器,然后再发送输出缓冲中的内容。

转载请一定注明来自:www.phpv.net

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