如果在执行php程序时看到这条警告:"Warning: Cannot modify header information - headers already sent by ...."

Few notes based on the following user posts:
有以下几种解决方法:

1. Blank lines (空白行):
Make sure no blank line after <?php ... ?> of the calling php scrīpt.
检查有<?php ... ?> 后面没有空白行,特别是include或者require的文件。不少问题是这些空白行导致的。

2. Use exit statement (用exit来解决):
Use exit after header statement seems to help some people
在header后加上exit();
header ("Location: xxx");
exit();

3. PHP has this annoying problem, if
your HTML goes before any PHP code or any header modification before
redirecting to certain page, it ll said "Warning: Cannot modify header
information - headers already sent by ...." Basically anytime you output
to browser, the header is set and cannot be modified. So two ways to
get around the problem:

3a. Use Javascrīpt (用Javascrīpt来解决):
<? echo "<scrīpt> self.location( file.php );</scrīpt>"; ?>
Since it s a scrīpt, it won t modify the header until execution of Javascrīpt.
可以用Javascrīpt来代替header。另外需要注意,采用这种方法需要浏览器支持Javascrīpt.

3b. Use output buffering (用输出缓存来解决):
<?php ob_start(); ?>
... HTML codes ...
<?php
... PHP codes ...
header ("Location: ....");
ob_end_flush();
?>
This
will save the output buffer on server and not output to browser yet,
which means you can modify the header all you want until the
ob_end_flush() statement. This method is cleaner than the Javascrīpt
since Javascrīpt method assumes the browser has Javascrīpt turn on.
However, there are overhead to store output buffer on server before
output, but with modern hardware I would imagine it won t be that big of
deal. Javascrīpt solution would be better if you know for sure your
user has Javascrīpt turn on on their browser.

就像上面的代码那样,这种方法在生成页面的时候缓存,这样就允许在输出head之后再输出header了。本站的许愿板就是采用这种方法解决的header问题。

4.set output_buffering = On in php.ini (开启php.ini中的output_buffering )
set
output_buffering = On will enable output buffering for all files. But
this method may slow down your php output. The performance of this
method depends on which Web server you re working with, and what kind of
scrīpts you re using.
这种方法和3b的方法理论上是一样的。但是这种方法开启了所有php程序的输出缓存,这样做可能影响php执行效率,这取决于服务器的性能和代码的复杂度。

第二种:

如何彻底杜绝warning: Cannot add header information - headers already sent
in…… 这种令人莫明其妙的的错误。
只要你写过PHP代码,相信都遇上过这个大多时候都令人莫明其妙的warning吧..今天我们就来搞定它……………
看了PHP手册,回答如下:消息“Warning: Cannot send session cookie - headers already
sent…”或者“Cannot add/modify header information - headers already sent…”。
函数 header(),setcookie() 和 session
函数需要在输出流中增加头信息。但是头信息只能在其它任何输出内容之前发送。在使用这些函数前不能有任何(如 HTML)的输出。函数
headers_sent() 能够检查您的脚本是否已经发送了头信息。请参阅“输出控制函数”。
意思是:不要在使用上面的函数前有任何文字,空行,回车,空格等。但。。。问题是,这答案并不令人满意。因为往往程序在其他PHP环境下运行却正常。
首先:这错误是怎么产生的呢?让我们来看看PHP是如何处理HTTP header输出和主体输
文讨论的是如何彻底杜绝warning: Cannot add header information - headers already sent in…… 这种令人莫明其妙的的错误。
只要你写过PHP代码,相信都遇上过这个大多时候都令人莫明其妙的warning吧..今天我们就来搞定它……………
看了PHP手册,回答如下:
消息“Warning: Cannot send session cookie - headers already sent…”或者“Cannot add/modify header information - headers already sent…”。

函数 header(),setcookie() 和 session
函数需要在输出流中增加头信息。但是头信息只能在其它任何输出内容之前发送。在使用这些函数前不能有任何(如 HTML)的输出。函数
headers_sent() 能够检查您的脚本是否已经发送了头信息。请参阅“输出控制函数”。

意思是:不要在使用上面的函数前有任何文字,空行,回车,空格等。但。。。问题是,这答案并不令人满意。因为往往程序在其他PHP环境下运行却正常。

首先:这错误是怎么产生的呢?让我们来看看PHP是如何处理HTTP header输出和主体输出的。

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

好!那我们来解决它:笨方法:把错误警告全不显示! 掩耳盗铃之计
error_reporting(E_ERROR | E_PARSE); 这里不要显示E_WARNING即可

解决方案:
1)适用于有权限编辑PHP。INI的人
打开php。ini文件(你应试比我清楚你的php。ini在哪里),找到
output_buffering =改为on或者任何数字。如果是IIS6,请一定改为ON,不然你的PHP效率会奇慢。

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

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

AllowOverride All
PHP_FLAG output_buffering On

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

那么,再用下面的方法:

在PHP文件的最开始加入:ini_set("output_buffering", "1");

让这个页面打开PHP的输出缓存。

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到浏览器,然后再发送输出缓冲中的内容。

4)绝杀技巧
     如果以上方法都不能等到满意的解决办法,请用如下办法:
    
     先用记事本打开出现问题的网页,另存为ANSI编码的同名文件。
     再用EditPlus将该文件另存为UTF-8编码的文件。
     再试试,应该可以显示了。
造成的原因主要由以下两点:
    一:在Header()函数之间输出了其他内容(一般由浏览器隐藏发送),导致了后来的Header不能再次发送新的页面类型。这可以通过开启Output_Buffering来解决,方法2)与3)就是这样。
   
二:PHP文件采用UTF-8编码,由于编码不兼容(特别是通过其他编码转换过来的),产生了BOM《在UCS 编码中有一个叫做"ZERO
WIDTH NO-BREAK
SPACE"的字符,它的编码是FEFF。而FFFE在UCS中是不存在的字符,所以不应该出现在实际传输中。UCS规范建议我们在传输字节流前,先传输
字符"ZERO WIDTH NO-BREAK
SPACE"。这样如果接收者收到FEFF,就表明这个字节流是Big-Endian的;如果收到FFFE,就表明这个字节流是Little-
Endian的。因此字符"ZERO WIDTH NO-BREAK SPACE"又被称作BOM。
  UTF-8不需要BOM来表明字节顺序,但可以用BOM来表明编码方式。字符"ZERO WIDTH NO-BREAK SPACE"的UTF-8编码是EF BB BF。所以如果接收者收到以EF BB BF开头的字节流,就知道这是UTF-8编码了。

  Windows就是使用BOM来标记文本文件的编码方式的。》导致了的头文件不能正确识别,这时只要去除UTF-8文件中的BOM就可以了,方法4)就是基于这种原理的。

出的。 PHP脚本开始执行时,它可以同时发送header(标题)信息和主体信息。 Header信息(来自 header() 或
SetCookie() 函数)并不会立即发送,相反,它被保存到一个列表中。 这样就可以允许你修改标题信息,包括缺省的标题(例如
Content-Type 标题)。但是,一旦脚本发送了任何非标题的输出(例如,使用 HTML 或 print()
调用),那么PHP就必须先发送完所有的Header,然后终止 HTTP
header。而后继续发送主体数据。从这时开始,任何添加或修改Header信息的试图都是不允许的,并会发送上述的错误消息之一。
好!那我们来解决它:笨方法:把错误警告全不显示! 掩耳盗铃之计 error_reporting(E_ERROR | E_PARSE);
这里不要显示E_WARNING即可 解决方案:
1)适用于有权限编辑PHP。INI的人打开php。ini文件(你应试比我清楚你的php。ini在哪里),找到 output_buffering
=改为on或者任何数字。如果是IIS6,请一定改为ON,不然你的PHP效率会奇慢。 2)使用虚拟主机,不能编辑PHP。INI,怎么办?
简单:在你的空间根目录下建立一个。htaccess文件,内容如下: AllowOverride All PHP_FLAG
output_buffering On 不幸的情况是:还是不行?全部网页都不能显示啦? 那么,再用下面的方法:
在PHP文件的最开始加入:ini_set("output_buffering", "1"); 让这个页面打开PHP的输出缓存。
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到浏览器,然后再发送输出缓冲中的内容。 4)绝杀技巧
如果以上方法都不能等到满意的解决办法,请用如下办法: 先用记事本打开出现问题的网页,另存为ANSI编码的同名文件。
再用EditPlus将该文件另存为UTF-8编码的文件。 再试试,应该可以显示了。 造成的原因主要由以下两点:
一:在Header()函数之间输出了其他内容(一般由浏览器隐藏发送),导致了后来的Header不能再次发送新的页面类型。这可以通过开启
Output_Buffering来解决,方法2)与3)就是这样。
二:PHP文件采用UTF-8编码,由于编码不兼容(特别是通过其他编码转换过来的),产生了BOM《在UCS 编码中有一个叫做"ZERO WIDTH
NO-BREAK
SPACE"的字符,它的编码是FEFF。而FFFE在UCS中是不存在的字符,所以不应该出现在实际传输中。UCS规范建议我们在传输字节流前,先传输
字符"ZERO WIDTH NO-BREAK
SPACE"。这样如果接收者收到FEFF,就表明这个字节流是Big-Endian的;如果收到FFFE,就表明这个字节流是Little-
Endian的。因此字符"ZERO WIDTH NO-BREAK SPACE"又被称作BOM。
  UTF-8不需要BOM来表明字节顺序,但可以用BOM来表明编码方式。字符"ZERO WIDTH NO-BREAK
SPACE"的UTF-8编码是EF BB BF。所以如果接收者收到以EF BB BF开头的字节流,就知道这是UTF-8编码了。
  Windows就是使用BOM来标记文本文件的编码方式的。》导致了的头文件不能正确识别,这时只要去除UTF-8文件中的BOM就可以了,方法4)
就是基于这种原理的。

来自:http://blog.csdn.net/besily/article/details/5396268

转载: PHP错误:Warning: Cannot modify header information - headers already sent by ...的更多相关文章

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

    这篇文章主要介绍了PHP错误Warning: Cannot modify header information - headers already sent by解决方法,需要的朋友可以参考下 今天在 ...

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

    PHP初学者容易遇到的错误:Warning: Cannot modify header information - headers already sent by ...: 通常是由不正确使用 hea ...

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

    一般来说在header函数前不能输出html内容,类似的还有setcookie() 和 session 函数,这些函数需要在输出流中增加消息头部信息.如果在header()执行之前有echo等语句,当 ...

  4. Warning: Cannot modify header information - headers already sent by ... functions_general.php on line 45

    摘自:有用到 http://blog.csdn.net/besily/article/details/5396268 PHP错误:Warning: Cannot modify header infor ...

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

    8月5日,第一个项目即将完成,测试时,发现登录功能会出现小问题:记住密码的时候会报错 Warning: Cannot modify header information - headers alrea ...

  6. 阿里云服务器出现Warning: Cannot modify header information - headers already sent by (output started at 问题的解决方法

    阿里云服务器出现Warning: Cannot modify header information - headers already sent by (output started at 问题的解决 ...

  7. 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 remo ...

  8. PHP:Cannot modify header information - headers already sent by错误的解决方案

    <?php ob_start();setcookie("username","test",time()+3600);echo "the user ...

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

    <?php ob_start(); setcookie("username","送家",time()+3600); echo "the user ...

随机推荐

  1. shell编程:for 循环

    hell 编程——for in 循环   -------for in 格式-------     for 无$变量 in 字符串 do $变量 done   一简单的字符串 枚举遍历法,利用for i ...

  2. 【delphi】delphi操作sqlite3

    SQLite SQLite是一个老牌的轻量级别的本地文件数据库,完全免费且开源,不需要安装,无须任何配置,当然,这样管理功能就不是很强大了,但是它的主要应用也是在本地数据库,可以说是最简单好用的嵌入式 ...

  3. java版本DbhelperMysql

    package com.hebut.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.P ...

  4. LeetCode: Validate Binary Search Tree 解题报告

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  5. C#学习笔记(7)——委托

    说明(2017-5-29 22:22:50): 1. 语法:public delegate void mydel();这一句在类外面,命名空间里面. 2. 专门新建一个方法,参数是委托: public ...

  6. 【C#】使用user32.dll的MessageBox弹窗消息

    要使用user32.dll的MessageBox弹窗消息,自然需要引入user32.dll到项目中. 一个最简单的实例如下: using System; using System.Runtime.In ...

  7. 微信小程序学习资料

    官方文档 https://mp.weixin.qq.com/debug/wxadoc/dev/ W3CSchool的教程 https://www.w3cschool.cn/weixinapp/ 更多参 ...

  8. Oracle 11gR2 ORA-12638 身份证明检索失败解决方法

    1. 找到Oracle安装目录或者菜单栏,打开Oracle Net manager 2. 选择本地 ->概要文件 ->下拉选择Oracle高级安全性 -> 取消NTS 3. 菜单栏  ...

  9. shell常用命令大全

    目录: 一.文件目录类命令 二.文件压缩和归档类命令 三.系统状态类命令 四.网络类命令 五.其他 一.文件目录类命令 1.查看联机帮助信息. man命令.#man ls info命令. #info ...

  10. redis关闭/重启服务器

    通过docker实现: 一.创建redis服务器与客户端 docker run -p : -d --name redis-server docker.io/redis: redis-server -- ...