如果在執行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 [...]就不會出現這類問題了。
http://www.bkjia.com/PHPjc/632112.htmlwww.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: 有以下幾種解...