Php 在数据库中存储会话

Php 在数据库中存储会话,php,mysql,session,Php,Mysql,Session,我编写这个helper类是为了在数据库中保存会话,但它似乎根本不起作用。我检查了session\u set\u save\u处理程序的返回值,它似乎总是返回一个假值,这意味着它首先无法设置handler函数。然后我尝试设置session.auto_start=0和session.save_handler='user',但这似乎没有改变任何事情。我是否可以在PHP.ini中更改其他内容以使其正常工作,或者问题出在我的类本身 class Session { private $db;

我编写这个helper类是为了在数据库中保存会话,但它似乎根本不起作用。我检查了session\u set\u save\u处理程序的返回值,它似乎总是返回一个假值,这意味着它首先无法设置handler函数。然后我尝试设置session.auto_start=0和session.save_handler='user',但这似乎没有改变任何事情。我是否可以在PHP.ini中更改其他内容以使其正常工作,或者问题出在我的类本身

 class Session 
 {
     private $db;

public function __construct ()
{
    //Instantiate new Database object
    $this->db = new Database ();

    // Set handler to overide SESSION
    $return = session_set_save_handler(
        array($this, "open"),
        array($this, "close"),
        array($this, "read"),
        array($this, "write"),
        array($this, "destroy"),
        array($this, "gc")
    );

            var_dump ($return);
    register_shutdown_function ('session_write_close') ;
    session_start ();  
}

/**
 * Open function
 * 
 * @param none
 * @return bool
 */
public function open ()
{
    if ($this->db) {
        return true;
    }
    return false;
}

    /**
 * Close function
 * 
 * @param none
 * @return bool
 */
public function close () 
{
    if($this->db->close ()) {
        return true;
    }

    return false;
}

/**
 * Read function
 * 
 * @param string $id
 * @return mixed
 */
public function read ($id)
{
    $this->db->query('SELECT data FROM sessions WHERE id = :id');
    $this->db->bind(':id', $id);

    if ($this->db->execute()) {
        $row = $this->db->single(); 
        return $row['data'];
    }

    return '';
}

/**
 * Write function
 * 
 * @param string $id
 * @param string $data
 * @return bool
 */
public function write ($id, $data)
{ 
    $access = time(); 
    $this->db->query('REPLACE INTO sessions VALUES (:id, :access, :data)');

    $this->db->bind(':id', $id);
    $this->db->bind(':access', $access);  
    $this->db->bind(':data', $data);

    if ($this->db->execute()){
        return true;
    }

    return false;
}

/**
 * Destroy function
 * 
 * @param string $id
 * @return bool
 */
public function destroy ($id)
{
  $this->db->query('DELETE FROM sessions WHERE id = :id');

  $this->db->bind(':id', $id);

  if ($this->db->execute ()) {
      return true;
  }

  return false;
} 

/**
 * Garbage collector function
 * 
 * @param int $maxLifeTime
 * @return bool
 */
public function gc ($maxLifeTime){
    $old = time() - $maxLifeTime;

    // Delete expired sessions from the database
    $this->db->query('DELETE * FROM sessions WHERE access < :old');     
    $this->db->bind(':old', $old);

    if($this->db->execute ()) {
        return true;
    }

    return false;
}
}

我通过查看下面的示例修复了它:


您的类似乎缺少一个
close
方法我注意到,添加了一个close方法,但仍然不起作用。。。
 CREATE TABLE `sessions` (
  `id` char(32) NOT NULL,
  `data` text NOT NULL,
  `access` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1;