« 簡單好用的工作備忘錄 | Main | 運動不只是運動 »

使用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編碼呈現,又不會出現上述問題。

Comments (4)

Calvin:

這是因為PHP不認得BOM(byte order mark), 這也是現在讓我很那悶的事情, 不曉得php官方是怎麼看待這個的!! 唉~~

界面与程式的分离,可以很好的避免这个问题。因为不再需要用UTF-8格式来编辑程式。

cefc:

微軟搞出來的BOM(byte order mark),應該由微軟去解決

cefc:

windows搞出來的BOM(byte order mark)應該由微軟去解決吧

Post a comment


 

寄信給我

About

This page contains a single entry from the blog posted on January 8, 2007 11:50 PM.

The previous post in this blog was 簡單好用的工作備忘錄.

The next post in this blog is 運動不只是運動.

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.
Powered by
Movable Type 3.33