使用setcookie()函數時總是報以下錯誤:
Warning: Cannot modify header information - headers already sent by....
解決辦法如下:
方法一:
在PHP裡Cookie的使用是有一些限制的。
1、使用setcookie必須在標籤之前
2、使用setcookie之前,不可以使用echo輸入內容
3、直到網頁被載入完後,cookie才會出現
4、setcookie必須放到任何資料輸出瀏覽器前,才送出
.....
由於上面的限制,在使用setcookie()函數時,學會遇到 "Undefined index"、"Cannot modify header information - headers already sent by"…等問題,解決辦法是在輸出內容之前,產生cookie,可以在程式的最上方加入函數 ob_start();
ob_start :開啟輸出緩衝區
函數格式:void ob_start(void)
說明:當緩衝區啟用時,所有來自PHP程式的非檔案頭資訊均不會發送,而是儲存在內部緩衝區。為了輸出緩衝區的內容,可以使用ob_end_flush()或flush()輸出緩衝區的內容。
方法二:
解決Warning: Cannot modify header information - headers already sent by ......
前幾天裝了個php的大頭貼系統測試,發現報錯Warning: Cannot modify header information - headers already sent by ......
今天又裝openads,還是出現這個問題。怒了。上網找了半天,有人說要在檔案開頭寫上
ob_start();
失敗。
後來開啟 php.ini 然後把 output_buffering 設為 on 。重起appache,OK。看來這才是解決辦法。
特別注意:
如果使用utf-8編碼,一定要去掉UTF-8中的BOM,這都是因為utf-8編碼檔案含有的bom原因,而php4,5都是不支援bom的。去掉bom,可以用Notepad++開啟轉換一下。
http://www.bkjia.com/PHPjc/319841.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/319841.htmlTechArticle使用setcookie()函數時總是報以下錯誤: Warning: Cannot modify header information - headers already sent by.... 解決辦法如下: 方法一: 在PHP裡Cookie的使用是...