« December 2006 | Main | February 2007 »

January 2007 Archives

January 8, 2007

使用PHP Session時潛在的可能錯誤

如果要在PHP中使用Session,通常第一個步驟就是啟用Session,也就是:

session_start();

但是用了之後很有可能會出現像這樣的錯誤訊息:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/codecrai/public_html/wiki/conf/dokuwiki.php:81) in /home/codecrai/public_html/wiki/inc/common.php on line 22

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/codecrai/public_html/wiki/conf/dokuwiki.php:81) in /home/codecrai/public_html/wiki/inc/common.php on line 22

這個問題的發生主要有兩種可能:

  1. 在session_start()之前有空格或空白斷行
  2. 把session_start()寫在<html>之後
不過如果你是用UTF-8的編碼方式來撰寫程式,也有可能造成上述第一項的情況(視編輯器而定),但是表面上卻看不出來,讓你在除錯時遲遲抓不著頭緒。此時只要轉換編碼方式為ANSI,問題立即獲得解決,以我使用的Notepad++為例(點圖放大):

utf8.jpg

這樣的設定既可讓網頁以UTF-8編碼呈現,又不會出現上述問題。

寄信給我

About January 2007

This page contains all entries posted to FuzzyWeblog in January 2007. They are listed from oldest to newest.

December 2006 is the previous archive.

February 2007 is the next archive.

Many more can be found on the main index page or by looking through the archives.

Creative Commons License
This weblog is licensed under a Creative Commons License.