当前位置:Gxlcms > PHP教程 > phpCannotmodifyheaderinformationheadersalreadysentby解_PHP教程

phpCannotmodifyheaderinformationheadersalreadysentby解_PHP教程

时间:2021-07-01 10:21:17 帮助过:28人阅读

如果在执行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();

3a. Use Javascrīpt (用Javascrīpt来解决):
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();
?>

另一篇文章

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.bkjia.comtruehttp://www.bkjia.com/PHPjc/632112.htmlTechArticle如果在执行php程序时看到这条警告:Warning: Cannot modify header information - headers already sent by .... Few notes based on the following user posts: 有以下几种解...

人气教程排行