【问题标题】:Singleton session problem单例会话问题
【发布时间】:2011-07-20 18:25:18
【问题描述】:

可能是一个快速修复,但无法弄清楚我哪里出错了。我正在设置一个简单的单例 Session 类,但出现以下错误,所以我显然没有正确设置:

我在这里犯了一个明显的错误吗?感谢您的帮助

警告:session_start() [function.session-start]:无法发送会话缓存限制器 - 标头已发送...

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;
    }
}

【问题讨论】:

标签: php oop singleton


【解决方案1】:

在调用session_start() 之前不能输出任何数据。在实例化该类之前,请确保没有回声或打印或任何吐出数据的东西。

【讨论】:

    【解决方案2】:

    也许这会有所帮助...

    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.
    

    【讨论】:

      【解决方案3】:

      headers already sent 错误的问题在于您发送了一些正文内容,html,也许是空格,......这个问题可以通过两种方式解决。

      1. Session 的创建成为脚本的第一件事 - 在任何输出操作之前通过调用 Session::instance() 一开始。

      2. 使用输出缓冲。第一条指令应该是ob_start(),最后一条是ob_end_flush()

      【讨论】:

        猜你喜欢
        • 2013-02-04
        • 2015-07-23
        • 1970-01-01
        • 2010-12-19
        • 1970-01-01
        • 1970-01-01
        • 2023-03-12
        • 2011-01-21
        • 2010-11-05
        相关资源
        最近更新 更多