WD1X.COM - 问答一下,轻松解决,电脑应用解决专家
主板显卡CPU内存显示器
硬盘维修显卡维修显示器维修
注册表系统命令DOS命令Win8
存储光存储鼠标键盘
内存维修打印机维修
WinXPWin7Win11Linux
硬件综合机箱电源散热器手机数码
主板维修CPU维修键盘鼠标维修
Word教程Excel教程PowerPointWPS
网络工具系统工具图像工具
数据库javascript服务器
PHP教程CSS教程XML教程

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

更新时间:2012-07-03 14:44 作者:佚名点击:

如果在执行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信息的试图都是不允许的,并会发送上述的错误消息之一。

顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
你可能感兴趣的内容