Warning: Cannot modify header information - headers already sent by (output started at

一般来说在header函数前不能输出html内容,类似的还有setcookie() 和 session 函数,这些函数需要在输出流中增加消息头部信息。如果在header()执行之前有echo等语句,当后面遇到header()时,就会报出 “Warning: Cannot modify header information - headers already sent by ....”错误。就是说在这些函数的前面不能有任何文字、空行、回车等,而且最好在header()函数后加上exit()函数。例如下面的错误写法,在两个php代码段之间有一个空行:

<?php

//some code here

?>

//这里应该是一个空行

<?php

header("http/1.1 403 Forbidden");

exit();

?>

原因是:PHP脚本开始执行 时,它可以同时发送http消息头部(标题)信息和主体信息. http消息头部(来自 header() 或SetCookie() 函数)并不会立即发送,相反,它被保存到一个列表中. 这样就可以允许你修改标题信息,包括缺省的标题(例如Content-Type 标题).但是,一旦脚本发送了任何非标题的输出(例如,使用 HTML 或 print()调用),那么PHP就必须先发送完所有的Header,然后终止 HTTPheader.而后继续发送主体数据.从这时开始,任何添加或修改Header信息的试图都是不允许的,并会发送上述的错误消息之一。

解决办法:

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

时间: 2024-08-28 11:56:35

Warning: Cannot modify header information - headers already sent by (output started at的相关文章

关于报错:Warning: Cannot modify header information - headers already sent by (output started at

8月5日,第一个项目即将完成,测试时,发现登录功能会出现小问题:记住密码的时候会报错 Warning: Cannot modify header information - headers already sent by (output started at 经过再三在百度上查询,终于查到错误的关键原因:[setcookie之前不可以有html标签内容].导致出现这种错误的方式有很多种,但惟独这种解释最让我理解. (因为百度知道的内容不可以复制了,只能贴出地址,有详细解释)地址[http://z

出现Warning: Cannot modify header information - headers already sent by ..的解决办法

这个错误的出现,原因是: 出这个错误是因为 header('Content-Type:text/html;charset= UTF-8');发送头之前不能有任何输出. 检查了下我的输出,错误信息如下: Warning: Cannot modify header information - headers already sent by (output started at /data/home/qxu1084910324/htdocs/bw_feature.php:1) in /data/hom

PHP错误Warning: Cannot modify header information - headers already sent by解决方法

这篇文章主要介绍了PHP错误Warning: Cannot modify header information - headers already sent by解决方法,需要的朋友可以参考下 今天在测试以下代码时遇到该错误: 复制代码代码如下: session_start();$_SESSION['username']=$username;echo "<script language='javascript'>location.href='../admin.php';</sc

PHP程序错误Warning: Cannot modify header information - headers already sent by ....

在使用phpmywind程序时出现的问题Warning: Cannot modify header information - headers already sent by .... 检查了许久,居然是编码的问题 请将单纯的utf-8格式改为utf-8无BOM编码格式 在NOTEPAD++的格式菜单可以修改 或者其它工具的

php5.6,Ajax报错,Warning: Cannot modify header information - headers already sent in Unknown on line 0

php5.6ajax报错 Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unkno

如何解决PHP 出现Warning: Cannot modify header information错误?

其实,出现这种情况一般都是因为程序修改过程中出现以下几个方面的错误操作导致: 原因一.UTF8编码和BOM冲突(最常见) 如果你登录后台出现类似Warning: Cannot modify header information – headers already sent by (output started at /www/wp-content/themes/pozhejun/function.php:1) in /www/wp-includes/pluggable.php on line 8

Cannot modify header information - headers already sent by出错的原因

<?php ob_start(); setcookie("username","送家",time()+3600); echo "the username is:".$HTTP_COOKIE_VARS["username"]."\n"; echo "the username is:".$_COOKIE["username"]."\n"; pri

PHP:Cannot modify header information - headers already sent by出错解决

<?php ob_start();setcookie("username","test",time()+3600);echo "the username is:".$HTTP_COOKIE_VARS["username"]."\n";echo "the username is:".$_COOKIE["username"]."\n";print

Warning: Cannot modify header information原因及解决方案

相信大多数人在写PHP代码的时候,都遇到过类似"Warning: Cannot send session cookie – headers already sent-"或者"Cannot add/modify header information – headers already sent-"这样的Warning提示.下面我们就来看看发生这个Warning的原因及解决方案. 函数 header(),setcookie() 和 session 函数需要在输出流中增加