Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 单例会话问题_Php_Oop_Singleton - Fatal编程技术网

Php 单例会话问题

Php 单例会话问题,php,oop,singleton,Php,Oop,Singleton,可能是一个快速解决方案,但无法找出我的错误所在。我正在设置一个简单的singleton会话类,但我得到了以下错误,因此我显然没有正确设置: 我是不是犯了一个明显的错误?谢谢你的帮助 警告:会话启动()[function.session start]:无法发送会话缓存限制器-已发送头 class Session { // Session singleton protected static $instance; private function __construct

可能是一个快速解决方案,但无法找出我的错误所在。我正在设置一个简单的singleton会话类,但我得到了以下错误,因此我显然没有正确设置:

我是不是犯了一个明显的错误?谢谢你的帮助

警告:会话启动()[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;
    }
}

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

这可能会有帮助


已发送的
标题的问题是,您发送了一些正文内容、html,可能是空白,
错误。。。可以使用两种方法消除此问题

  • 会话的创建成为脚本的首要任务之一-在开始任何输出操作之前,通过调用
    会话::实例()

  • 使用输出缓冲。第一条指令应该是
    ob\u start()
    ,最后一条
    ob\u end\u flush()


  • 这个重复太多了。投票结果接近。而且,这与单身没有什么关系。
    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.