雜念 - 老男人的碎念

WordPress Warning: Cannot modify header information – headers already sent by

李小政自己在轉移SERVER時,遇到了Cannot modify header information的問題

在使用了底下1,2兩項以後,就把問題搞定了!但是也有遇到朋友是沒法子搞定的

在這邊找到了一篇蠻不錯的文章,特底在這邊備份一下

原文在此

轉載自Eric’s Who Know

全文如下

01. 如果要存檔成 UTF-8, 千萬別跟自己過不去, 去使用 Windows 內建的記事本, 請使用 PSPad,NotePad++, EmEditor, UltraEdit 或其它好一點的文字編輯器。因為使用 Windows 內建的記事本, 以UTF-8存檔時, 會自動添加 BOM 標籤, 而造成一些問題, 如 Warning: Cannot modify header information – headers already sent by…。

02. 以下由易達成性來做排序

  a. 使用 PSPad, NotePad++, EmEditor, UltraEdit 或其它的好一點的文字編輯器, 來另存成 UTF-8 且選擇不要添加 BOM 標籤。

PSPad : 格式 -> UTF-8 -> 存檔
UltraEdit : 檔案 -> 另存新檔 -> 格式選 UTF-8 無 BOM
NotePad++ : 格式 -> 編譯成 UTF-8碼 (檔首無 BOM) -> 存檔
EmEditor : 檔案 -> 另存新檔 -> 編碼選 UTF-8, 下方的加入一個 Unicode 籤名 (BOM) 不要打勾

  b. 檢查 wp-config.php 的 <?php 前, 和 ?> 後是否有多於的空白或空行。

  c. 使用 Windows 內建的記事本打開 wp-config.php, 然後直接另存新檔, 編碼請選擇 ANSI。

  d. 到 WordPress.zipWordPress.tar.gz 下載最新版本的 WordPress, 解壓出 wp-config-sample.php, 改檔名成 wp-config.php, 再依您實際狀況, 來修改 wp-config.php 裡面的設定。

  e. 於 php.ini 添加或修改

1
output_buffering = On

1
output_buffering = 4096

// 如果您使用的是虛擬主機, 且可以修改 php.ini, 那就在 php.ini 添加上面兩句其中一句; 如果主機是自己的, 那就打開 php.ini, 把 output_buffering 設定成 On 或 4096。

; Output buffering allows you to send header lines (including cookies) even
; after you send body content, at the price of slowing PHP’s output layer a
; bit. You can enable output buffering during runtime by calling the output
; buffering functions. You can also enable output buffering for all files by
; setting this directive to On. If you wish to limit the size of the buffer
; to a certain size – you can use a maximum number of bytes instead of ‘On’, as
; a value for this directive (e.g., output_buffering=4096).
output_buffering = Off

03. 為什麼這個問題大部分發生在大陸用戶呢? 分別看了 CNTW, 就不難發現

a. CN 的做法是: WordPress en + zh_CN.mo, zh_CN.po, 翻譯過的 wp-config-sample.php, readme.html, setup-config.php 的整合包
b. TW 的做法是: WordPress en + zh_TW.mo, zh_TW.po 語言包

如果您用好一點的文字編輯器, 或者是讓安裝程式自動由 wp-config-sample.php 產生 wp-config.php, 那您可能不會遇到這個問題, 還會感覺 CN 真貼心, 連安裝過程都翻譯成您熟悉的語言了。

04. 如果您 wp-config.php 都打開了, 那不妨順便添加下面 2 句

1
define('WP_POST_REVISIONS', false);

2
define('AUTOSAVE_INTERVAL', 86400);

  a. AUTOSAVE_INTERVAL : 用來設定自動儲存的時間間隔, 單位為 sec 秒。

  b. WP_POST_REVISIONS 用來設定文章版本數 : false 那邊可以改成 -1 (同 true), 0 (同 false), >1 的整數, 如果您希望每個修改的版本都儲存下來; 那就設定成 -1 (同 true), 如果您希望關閉文章版的的功能, 那就設定成 0 (同 false); 如果您希望只要保存一固定數量, 就設定成 >1 的整數。

true (default), -1: store every revision
false, 0: do not store any revisions (except the one autosave per post)
(int) > 0: store that many revisions (+1 autosave) per post. Old revisions are automatically deleted.

如果之前的文章版本已經積的很多了, 可以利用 phpMyAdmin 來刪除, 不過記得先備份資料庫喔。
// 也可以用 Delete-Revision 這個插件來做刪除。

1
DELETE FROM wp_posts WHERE post_type = "revision";

// 沒有說一定要用 phpMyAdmin 才能刪除, 而是一般虛擬主機大多提供的是 phpMyAdmin 所以 …, 如果要用 MySQL command-line 或其它的工具也是可以的喔。

  c. 另外, 如果出現 Fatal error: Allowed memory size of xxx bytes exhausted (tried to allocate xxx bytes) in … 則可以加上下面那句, 如果 64M 還是不夠就把數字再改大一點。但是如果您的主機商有限制 Memory limit 上限, 那就只能停用一些較沒再使用的外掛。

1
define('WP_MEMORY_LIMIT', '64M');

// 有些人會去改 ./wp-settings.php, 但每升級一次就要改一次, 所以改 wp-config.php 會比較好。

1
if ( !defined('WP_MEMORY_LIMIT') )

2
define('WP_MEMORY_LIMIT', '32M');

// 另外, 修改 php.ini 也是一個辦法

1
memory_limit = 16M

// 最後, 修改 .htaccess file 也可行, 加上

1
php_value memory_limit 96M

// 如果您的虛擬主機有安裝 SuPHP, 則 .htaccess file 請改成

1
memory_limit = 96M

Ref:
01. UTF-8, UTF-16, UTF-32 & BOM

Tags: .htaccess, php.ini, SuPHP, UTF-8, WordPress, wp-config

5 評論

歡迎留言聊天

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料