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 information - headers already sent by ...
如果在执行php程序时看到这条警告:"Warning: Cannot modify header information - headers already sent by ...."
Few notes based on the following user posts:
有以下几种解决方法:
ANSI先看看不带bom
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)就是基于这种原理的。
Warning: Cannot modify header information - headers already sent by ... functions_general.php on line 45的更多相关文章
- 阿里云服务器出现Warning: Cannot modify header information - headers already sent by (output started at 问题的解决方法
阿里云服务器出现Warning: Cannot modify header information - headers already sent by (output started at 问题的解决 ...
- PHP错误Warning: Cannot modify header information - headers already sent by解决方法
这篇文章主要介绍了PHP错误Warning: Cannot modify header information - headers already sent by解决方法,需要的朋友可以参考下 今天在 ...
- Warning: Cannot modify header information - headers already sent by (output started at
一般来说在header函数前不能输出html内容,类似的还有setcookie() 和 session 函数,这些函数需要在输出流中增加消息头部信息.如果在header()执行之前有echo等语句,当 ...
- 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 ...
- 转载: PHP错误:Warning: Cannot modify header information - headers already sent by ...
如果在执行php程序时看到这条警告:"Warning: Cannot modify header information - headers already sent by ....&quo ...
- PHP 错误:Warning: Cannot modify header information - headers already sent by ...
PHP初学者容易遇到的错误:Warning: Cannot modify header information - headers already sent by ...: 通常是由不正确使用 hea ...
- 关于报错:Warning: Cannot modify header information - headers already sent by (output started at
8月5日,第一个项目即将完成,测试时,发现登录功能会出现小问题:记住密码的时候会报错 Warning: Cannot modify header information - headers alrea ...
- php有些系统会报错或提示 Cannot modify header information - headers already sent by
Warning: Cannot modify header information - headers already sent by (output started at /home/test/do ...
- PHP:Cannot modify header information - headers already sent by错误的解决方案
<?php ob_start();setcookie("username","test",time()+3600);echo "the user ...
随机推荐
- 使用计算监控(Using computed observables)
计算监控(Computed Observables) 如果有两个监控属性firstName, lastName,此时我们要显示full name,我们要怎么办呢? 这时,可以创建一个computed ...
- 关于oracle数据库(10)函数
分析函数,用于统计排名 语法:函数名() over(order by 排序字段 asc | desc) row_number() 无论值是否相等,生成连续的行号 -- 1,2,3,4, select ...
- 移动端解决input focus后键盘弹出,高度被挤压的问题
//解决弹出键盘页面高度变化bug var viewHeight = window.innerHeight; //获取可视区域高度 $("input").focus(functio ...
- POJ 2418 Hardwood Species (哈希,%f 和 %lf)
我的错因: 本来改用%f输出,我用了%lf,结果编译器直接判定为错误(一部分编译器认为lf是没有错的).当时我还以为是hash出错了.. 方法不止一种: 方法 时间 空间 Hash 891ms 5 ...
- Python基础学习7---异常处理
处理异常 我们可以使用 try..except 语句来处理异常.我们把通常的语句放在try-块中,而把我们的错误处理语句放在except-块中. import sys try: s = raw_inp ...
- Ubuntu基本命令--apt, dpkg
一.Ubuntu中软件安装方法 1.APT方式 (1)普通安装:apt-get install softname1 softname2 …; (2)修复安装:apt-get -f install so ...
- js第一天 innerHTML和value 的区别
innerHTML在JS是双向功能:获取对象的内容 或 向对象插入内容:如:<div id="aa">这是内容</div> ,我们可以通过 document ...
- why is agreement hard in a distributed system?
same question as: why is PAXOS necessary? 1, what if >1 nodes become leaders simultaneously? that ...
- NSBundle的用法
bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in). 对应bundle,cocoa提供了类NSBu ...
- javascript克隆一个对象
/* * 克隆一个对象 */ com.ty.repairtech.JsonOperation.clone = function(obj) { // Handle the 3 simple types, ...