PHP头条
热点:

php Cannot modify header informationheaders 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();

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();
?>

另一篇文章

<?php
ob_start();
setcookie("username","宋岩宾",time()+3600);
echo "the username is:".$HTTP_COOKIE_VARS["username"]."n";
echo "the username is:".$_COOKIE["username"]."n";
print_r($_COOKIE);
?>
Warning: Cannot modify header information - headers already sent by出错的原因
我在php程序的头部加了,

header("cache-control:no-cache,must-revalidate");

之后页面就出现上面的错误,看了N个资料也没有结果。今天偶尔发现原来是我的php.ini里面的配置出了问题,在C:windows下找到php.ini文件
output_buffering默认为off的。

小提示,还有一个更好的解决办法就是在php.ini 然后把 output_buffering 设为 on [...]就不会出现这类问题了。

www.phpzy.comtrue/php/26249.htmlTechArticlephp Cannot modify header informationheaders already sent by解决方法 如果在执行php程序时看到这条警告:Warning: Cannot modify header information - headers already sent by .... Few notes based on the following user posts: 有以...

相关文章

    暂无相关文章

PHP之友评论

今天推荐