2011-07-20 4 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

これが多すぎます。投票が終わった。また、これはシングルトンとほとんど関係がありません。 [既に送信されたヘッダー](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、あなたには、いくつかの本文の内容を送信したということです、多分空白、...この問題は、することができ2つの方法で削除されました。

  1. Sessionの作成は、スクリプトの最初のものの一つとする - 任意の出力操作の前に初めにSession::instance()を呼び出して。

  2. 出力バッファリングを使用します。最初の命令はob_start()で、最後の命令はob_end_flush()である必要があります。

関連する問題