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 ...
随机推荐
- 【Machine Learning in Action --4】朴素贝叶斯过滤网站的恶意留言
背景:以在线社区的留言板为例,为了不影响社区的发展,我们需要屏蔽侮辱性的言论,所以要构建一个快速过滤器,如果某条留言使用了负面或者侮辱性的语言,那么就将该留言标识为内容不当.过滤这类内容是一个很常见的 ...
- 设n是奇数,证明:16|(n4+4n2+11)(整除原理1.1.1)
设n是奇数,证明:16|(n4+4n2+11) 解: 令n=2k+1,k∈z n4+4n2+11 =(2k+1)4+4(2k+1)2+11 =(4k2+4k+1)2+(2k+1)2+11 =16k4+ ...
- css遗漏
对于float浮动 子级元素浮动之后,因为元素脱离了文档流所以父级元素的高度不会auto而是变成0的解决方案 父级元素增加伪类 父级:after{ content:""; disp ...
- 深入体会__cdecl与__stdcall
在学习C++的过程中时常碰到WINAPI或者CALLBACK这样的调用约定,每每觉得十分迷惑.究竟这些东西有什么用?不用他们又会不会有问题?经过在网上的一番搜寻以及自己动手后,整理成以下的学习笔记.1 ...
- java并发编程框架 Executor ExecutorService invokeall
首先介绍两个重要的接口,Executor和ExecutorService,定义如下: public interface Executor { void execute(Runnable command ...
- 关于malloc和free函数的用法
原文:http://blog.pfan.cn/vfdff/33507.html 个人总结 在C语言的学习中,对内存管理这部分的知识掌 握尤其重要!之前对C中的malloc()和free()两个函数的了 ...
- 关于js中原型链的理解
我们创建的每个函数都有一个prototype(原型)属性,这个属性是一个指针,一个对象.无论什么时候,我们只要创建一个新函数,就会根据一组特定的规则为该函数创建一个prototype属性,这个属性对象 ...
- Bootstrap 3 与 Foundation 5
开发工程师, 使用 Bootstrap. 前端开发人员, 使用 Foundation. 我们来谈谈为什么. Bootstrap 与 Foundation 有许多关键的区别, 但是, 我想你只需要记住一 ...
- 初识Iaas,paas
Iaas(Infrastructure-as-a-service),直译为基础设备作为一种服务. Paas(Platform as a service),直译为平台作为一种服务. 暂且忘掉这两个单词, ...
- 配置php时。提示的错误session_start(): Failed to initialize storage module解决办法
当浏览器输入访问地址后 报这样的错时----session_start(): Failed to initialize storage module 进入到此目录vi /usr/local/php/e ...