欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php Cannot modify header informationheaders already sent by解_PHP教程

程序员文章站 2022-05-01 11:37:45
...
如果在执行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来解决):
echo " self.location( file.php );ī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 (用输出缓存来解决):

... 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: 有以下几种解...