类会话在php 5.4中警告session_start()

时间:2014-09-04 16:22:56

标签: php class session

我在我的文件中有发行者php获取index.php消息 警告:session_start():无法发送会话缓存限制器 - 已在/ Users / [username] / Sites / application /中发送的标头(在/Users/[username]/Sites/application/Core/users.php:3处开始输出)第20行的Core / session.php

<?php
// A class to help work with Sessions
// In our case, primarily to manage logging users in and out

// Keep in mind when working with sessions that it is generally 
// inadvisable to store DB-related objects in sessions


require_once('includes/initialize.php');

class Session{


    private $logged_in=false;
    public $id;


    function __construct() {
        //@ob_start();
        session_start();
        $this->check_login();

        if($this->logged_in) {

        }else{

        }


    }


    public function is_logged_in() {
       return $this->logged_in;
     }

    public function login($username) {
     // database should find user based on username/password
         if($username){
           $this->id = $_SESSION['id'] = $username->id;
           $this->logged_in = true;
         }
    }


    public function logout() {
      unset($_SESSION['id']);
      unset($this->id);
      $this->logged_in = false;
    }



    private function check_login() {
        if(isset($_SESSION['id'])) {
          $this->id = $_SESSION['id'];
          $this->logged_in = true;
        } else {
          unset($this->id);
          $this->logged_in = false;
        }
    }
}

$session = new Session();

?>

1 个答案:

答案 0 :(得分:1)

必须先调用{p> session_start();。您需要将其从构造函数中取出并放在require_once语句之前。