错误提示:Cannot modify header information - headers already sent by ....
原文地址:
http://www.phpchina.com/html/47/26247-8026.html
昨晚在转换编码的时候,有一个页面需要在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 <?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执行效率,这取决于服务器的性能和代码的复杂度。
分享到:
相关推荐
php出现Cannot modify header information的解决方法
Warning: Cannot modify header information - headers already sent by出错的原因
使用setcookie()函数时总是报以下错误: Warning: Cannot modify header information – headers already sent by…. 解决办法如下: 方法一: 在PHP里Cookie的使用是有一些限制的。 1、使用setcookie必须在<...
今天在测试以下代码时遇到该错误: 复制代码 代码如下: ...Warning: Cannot modify header information – headers already sent by… 看了一些网上的方法也没解决,最后在php.ini配置output_buffering默认为4096
以下是对PHP中的Cannot modify header information问题的解决方法进行了详细的分析介绍,需要的朋友可以过来参考下
本文实例讲述了PHP提示Cannot modify header information – headers already sent by解决方法,是进行PHP程序设计过程中经常会遇到的问题。本文对此以实例形式分析解决方法。分享给大家供大家参考。具体方法如下: ...
主要介绍了PHP错误Warning: Cannot modify header information - headers already sent by解决方法,需要的朋友可以参考下
本篇对setcookie中Cannot modify header information-headers already sent by错误的解决方法进行了详细的分析介绍。需要的朋友参考下
主要介绍了PHP提示Cannot modify header information - headers already sent by解决方法,是在PHP程序开发中非常典型的错误情况,非常具有实用价值,需要的朋友可以参考下
Warning: Cannot modify header information – headers already sent by (output started at…… 这种提示,彼岸世界上的处理是“解决方法为:在functions.php 的最上面一行 添加函数 ob_start(); 即可”,但实际试...
用PHP header()函数实现页面404错误提示功能,特整理了PHP header()函数的详细使用分享给大家,需要大家多多支持我们
用途:主要用于整站所有文件为utf8无bom文件,个别文件却存bom,而造成的如:Warning: Cannot modify header information - headers already sent 等的错误。 如何解决BOM:先放在服务器可执行目录下,使用 浏览器http://...
这涉及到4种头标类型: Last-Modified(最后修改时间); Expires(有效期限); Pragma(编译指示); Cache-Control(缓存控制);...头标Last-Modified使用UTC日期时间值。...可以这样避免对页面进行缓存: header(“Pra
port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in D Cannot modify header information - headers already sent by (output started at D100) in D Cannot modify header ...
可是我想大多说都是没有问题,如果出现了Cannot modify header information - headers already sent这样的警告,这是是说不能修改头部信息,头部信息已经发送。下来就了解一下为什么会出现两种不同结果? 缓冲区 做个...