解决Warning: Cannot modify header information - headers already sent b...
解決Warning: require(E:\testwwwroot\cc06\wp-admin/wp-includes/compat.php) [function.require]: failed to open stream: No such file or directory in E:\testwwwroot\cc06\wp-admin\wp-settings.php on line 246
Fatal error: require() [function.require]: Failed opening required 'E:\testwwwroot\cc06\wp-admin/wp-includes/compat.php' (include_path='.;C:\php5\pear') in E:\testwwwroot\cc06\wp-admin\wp-settings.php on line 246。
1. Blank lines (空白行): Make sure no blank line after <?php ... ?> of the calling php script. 檢查有<?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 Javascript (用Javascript來解決): <? echo "<script> self.location(\"file.php\");</script>"; ?> Since it's a script, it won't modify the header until execution of Javascript. 可以用Javascript來代替header。但是上面的這段代碼我沒有執行成功... 另外需要注意,采用這種方法需要瀏覽器支持Javascript.
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 Javascript since Javascript method assumes the browser has Javascript 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.?? Javascript solution would be better if you know for sure your user has Javascript turn on on their browser.
就像上面的代碼那樣,這種方法在生成頁面的時候緩存,這樣就允許在輸出head之后再輸出header了。本站的許愿板就是采用這種方法解決的header問題。
http://www.discuz.net/viewthread.php?tid=70318
在后臺管理或者有時候在論壇,點擊一個頁面,頁頂會出現 Warning: Cannot modify header information - headers already sent by.... 這類語句,造成這個原因是因為setcookie語句的問題。cookie本身在使用上有一些限制,例如: 1.呼叫setcookie的敘述必須放在<html>標籤之前 2.呼叫setcookie之前,不可使用echo 3.直到網頁被重新載入後,cookie才會在程式中出現 4.setcookie函數必須在任何資料輸出至瀏覽器前,就先送出 5.…… 基於上面這些限制,所以執行setcookie()函數時,常會碰到"Undefined index"、"Cannot modify header information - headers already sent by"…等問題,解決"Cannot modify header information - headers already sent by"這個錯誤的方法是在產生cookie前,先延緩資料輸出至瀏覽器,因此,您可以在程式的最前方加上ob_start();這個函數。這樣就可以解決了。
?
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 scripts you're using. 這種方法和3b的方法理論上是一樣的。但是這種方法開啟了所有php程序的輸出緩存,這樣做可能影響php執行效率,這取決于服務器的性能和代碼的復雜度。
?
?
WordPress的程序執行時會首先調用wp-config.php一類的配置文件,也會調用wp-db.php建立數據庫連接以備后用。這些文件只是做一些設置,并不輸出html代碼。設置完了后,程序本身開始執行了,有些程序會使用header命令設置一個HTTP頭。由于HTTP頭必須在html代碼輸出之前設置好,否則html代碼已經開始往客戶端發送了,HTTP也就已經發送過了,沒法追回來重新設置了。WordPress CodeX里對這個問題作出了說明:《How do I solve the Headers already sent warning problem?》。文章指出:要確保各個文件——尤其是經常被編輯的wp-config.php文件——以<?php開頭,以?>結尾,前后不能有其他字符。具體到上面的例子,很明顯,提示信息說wp-config.php的第一行就開始了html輸出,這有可能是第一行的<?php部分前面被加上的其他字符,比如空格一類的。再聯想到以前提到的BOM的問題,那么非常有可能是因為這位朋友使用了Windows的記事本編輯了wp-config.php文件并保存成了UTF-8編碼的文檔,從而因為BOM的三個字符的輸出造成了header命令執行出錯。
WordPress中文論壇沒有提供全文搜索的功能,只能搜索標題,所以我用Google搜索了一下Cannot modify header information site:wordpress.org.cn,好像碰到這個問題的人還真不少。目前大家用的WordPress主要是WordPress英文原版和幾個WordPress中文版。我的中文包又不包含wp-config-sample.php文件,自然不關我的事;WordPress原版用的ASCII碼,自然不包含BOM,也不會出這樣的錯誤;xigang制作的WordPress中文版在WordPress中文論壇有下,我去下載了WordPress 2.0.4和2.0.3這兩個,檢查了一下,沒有問題;點點游的WordPress 2.0.4中文版里,wp-config-sample.php文件用的是GB2312編碼和DOS行尾符,GOD!不過這樣也好,如果有人用記事本修改了這個文件,DOS行尾符不會造成編輯問題,GB2312編碼不會造成BOM的問題,呼。
唉,如果你要用WordPress架Blog,還是扔掉記事本,裝個UltraEdit或者EditPlus吧!
Update:那些提示(output started at \wp-includes\wp-db.php:104)的,我打開wp-db.php文件看了一下,104行好像是輸出數據庫錯誤的呀。還是檢查一下你的數據庫連接,還有是不是IIS上才會有這個問題呀?
轉載于:https://www.cnblogs.com/wangchunming/archive/2012/08/29/2662335.html
總結
以上是生活随笔為你收集整理的解决Warning: Cannot modify header information - headers already sent b...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu开发之旅一---安装初步
- 下一篇: android webview控件的缩放