关于 Cannot modify header information 的解决
来源:百度文库 编辑:神马文学网 时间:2024/04/30 03:49:48
昨晚在转换编码的时候,有一个页面需要在head中申明utf-8的编码,但是这与程序中的一处header产生了冲突。google了一下,找到几种解决方法,翻译整理一下:
If you got this message: "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:
有以下几种解决方法:
1. Blank lines (空白行):
Make sure no blank line after of the calling php scrīpt.
检查有 后面没有空白行,特别是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 yourHTMLgoes before any PHP code or any header modification before redirectingto certain page, it ll said "Warning: Cannot modify header information- headers already sent by ...." Basically anytime you output tobrowser, the header is set and cannot be modified. So two ways to getaround the problem:
3a. Use Javascrīpt (用Javascrīpt来解决):
echo " self.location( file.php ); "; ?>
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 (用输出缓存来解决):
... HTML codes ...
... PHP codes ...
header ("Location: ....");
ob_end_flush();
?>
Thiswill save the output buffer on server and not output to browser yet,which means you can modify the header all you want until theob_end_flush() statement. This method is cleaner than the Javascrīptsince Javascrīpt method assumes the browser has Javascrīpt turn on.However, there are overhead to store output buffer on server beforeoutput, but with modern hardware I would imagine it won t be that bigof deal. Javascrīpt solution would be better if you know for sure youruser has Javascrīpt turn on on their browser.
就像上面的代码那样,这种方法在生成页面的时候缓存,这样就允许在输出head之后再输出header了。本站的许愿板就是采用这种方法解决的header问题。
4.set output_buffering = On in php.ini (开启php.ini中的output_buffering )
setoutput_buffering = On will enable output buffering for all files. Butthis method may slow down your php output. The performance of thismethod depends on which Web server you re working with, and what kindof scrīpts you re using.
这种方法和3b的方法理论上是一样的。但是这种方法开启了所有php程序的输出缓存,这样做可能影响php执行效率,这取决于服务器的性能和代码的复杂度。
If you got this message: "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:
有以下几种解决方法:
1. Blank lines (空白行):
Make sure no blank line after of the calling php scrīpt.
检查有 后面没有空白行,特别是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 yourHTMLgoes before any PHP code or any header modification before redirectingto certain page, it ll said "Warning: Cannot modify header information- headers already sent by ...." Basically anytime you output tobrowser, the header is set and cannot be modified. So two ways to getaround the problem:
3a. Use Javascrīpt (用Javascrīpt来解决):
echo "
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 (用输出缓存来解决):
... HTML codes ...
... PHP codes ...
header ("Location: ....");
ob_end_flush();
?>
Thiswill save the output buffer on server and not output to browser yet,which means you can modify the header all you want until theob_end_flush() statement. This method is cleaner than the Javascrīptsince Javascrīpt method assumes the browser has Javascrīpt turn on.However, there are overhead to store output buffer on server beforeoutput, but with modern hardware I would imagine it won t be that bigof deal. Javascrīpt solution would be better if you know for sure youruser has Javascrīpt turn on on their browser.
就像上面的代码那样,这种方法在生成页面的时候缓存,这样就允许在输出head之后再输出header了。本站的许愿板就是采用这种方法解决的header问题。
4.set output_buffering = On in php.ini (开启php.ini中的output_buffering )
setoutput_buffering = On will enable output buffering for all files. Butthis method may slow down your php output. The performance of thismethod depends on which Web server you re working with, and what kindof scrīpts you re using.
这种方法和3b的方法理论上是一样的。但是这种方法开启了所有php程序的输出缓存,这样做可能影响php执行效率,这取决于服务器的性能和代码的复杂度。
关于 Cannot modify header information 的解决
bytea - "Cannot modify header information"的解决方法
转贴:Cannot open precompiled header file:'Debug...
编译C 出现错误:Cannot open precompiled header file
HTTP的Header信息
关于“cannot find a valid peer process to connect to”错误的解决方案
关于电脑上各盘里的System Volume Information文件夹的分析
HTTP的一些参考资料和Header信息
Exceed PHP - header 函式的使用
html中header的内容 - 猛男的日志 - 网易博客
关于解决失地农民社会保障问题的建议
关于解决农民工问题的若干意见
《关于解决睡觉问题的重要讲话》
关于要求解决经费困难的申请
转帖]关于cidaemon.exe进程CUP占用率高的解决
关于cidaemon.exe进程CUP占用率高的解决
关于认真解决住房问题的六点建议
关于认真解决住房问题的六点建议
Jsp页面中关于客户端图片缓存的解决
关于《水浒传》的资料 - 已解决 - 搜搜问问
关于《水浒传》的资料 - 已解决 - 搜搜问问
关于红酒的知识 - 已解决 - 搜搜问问
关于XP系统不能访问网上邻居的全面解决专案
关于网页按钮和一部分图片无法显示的解决