2011-07-20 27 views
0

可能是一個快速修復,但無法弄清楚我要去哪裏出錯。我正在設置一個簡單的單身Session類,但我得到以下錯誤,所以我顯然沒有正確設置:單身會話問題

我在這裏犯了一個明顯的錯誤嗎?感謝您的幫助

警告:在session_start()[function.session啓動]:無法發送會話緩存限制器 - 已經發送了頭......

class Session { 

    // Session singleton 
    protected static $instance; 

    private function __construct() 
    { 
     //start the session 
     session_start(); 

     Session::$instance = $this; 
    } 

    public static function instance() 
    { 
     if (Session::$instance === NULL) 
     { 
      // Create a new instance 
      new Session; 
     } 

     return Session::$instance; 
    } 
} 
+0

這太多了。關閉投票。此外,這與單身人士沒什麼關係。 [Headers already sent](http://stackoverflow.com/questions/2938777/headers-already-sent) – webbiedave

回答

1

不能輸出任何數據之前調用session_start() 。在實例化該類之前,確保沒有回聲或打印或任何吐出數據的東西。

0

也許這將幫助...

http://php.net/manual/en/function.session-start.php

For the error: 

Warning: session_start(): Cannot send session cache limiter - headers already sent ... 

this kind of errors would occur, when the encoding of your script file needs to send some headers just after your script starts to execute, 

this happens mostly with the scripts using normal utf8 encoding. 

To overcome the issue, use utf8(without BOM) encoding provided by notepad++ and most modern editors. Using utf8 encoding and cookie based sessions, will result in headers already sent by error. 
0

headers already sent錯誤的問題是,你已經派出一些主體內容,HTML,也許空白,......這個問題可以使用兩種方式刪除。

  1. Session的創建是腳本的第一件事情之一 - 任何輸出操作之前,通過在開始通話Session::instance()

  2. 使用輸出緩衝。第一條指令應該是ob_start(),最後是ob_end_flush()