使用PDO的php会话类 - 获取错误

时间:2012-02-26 09:30:37

标签: php session pdo

我的会话课程:

<?php

include_once('db.php');

class PDOSession
{
    protected $pdo;
    protected $table = 'SessionData';

    public function __construct()
    {
        // Get a database connection
        $db = new PDOConnectionFactory();
        $this->pdo = $db->getConnection(true);

        // Start session
        session_set_save_handler(array(__CLASS__, '_open'),
                                 array(__CLASS__, '_close'),
                                 array(__CLASS__, '_read'),
                                 array(__CLASS__, '_write'),
                                 array(__CLASS__, '_destroy'),
                                 array(__CLASS__, '_gc'));
        session_start();
    }

    public function __destruct()
    {
        session_write_close();
    }

    protected function fetchSession($id)
    {
        $stmt = $this->pdo->prepare('SELECT id, data FROM '.$this->table.' WHERE id = :id AND unixtime > :unixtime');
        $stmt->execute(array(':id' => $id, ':unixtime' => (time() - (int)ini_get('session.gc_maxlifetime'))));
        $sessions = $stmt->fetchAll();

        return empty($sessions) ? false : $sessions[0];
    }

    protected function _open($savePath, $sessionName)
    {
        return true;
    }

    protected function _close()
    {
        return true;
    }

    protected function _read($id)
    {
        $session = $this->fetchSession($id);
        return ($session === false) ? false : $session['data'];
    }

    protected function _write($id, $sessionData)
    {
        $session = $this->fetchSession($id);
        if($session === false) {
            $stmt = $this->pdo->prepare('INSERT INTO '.$this->table.' (id, data, unixtime) VALUES (:id, :data, :time)');
        } else {
            $stmt = $this->pdo->prepare('UPDATE '.$this->table.' SET data = :data, unixtime = :time WHERE id = :id');
        }
        $stmt->execute(array(
                        ':id' => $id,
                        ':data' => $sessionData,
                        ':time' => time()
                        ));
    }

    protected function _destroy($id)
    {
        $stmt = $this->pdo->prepare('DELETE FROM '.$this->table.' WHERE id = :id');
        $stmt->execute(array(':id' => $id));
    }

    protected function _gc($maxlifetime)
    {
        $stmt = $this->pdo->prepare('DELETE FROM '.$this->table.' WHERE unixtime < :time');
        $stmt->execute(array(':time' => (time() - (int) $maxlifetime)));
    }
}
$newPDOSessionStartHere = new PDOSession();

我的错误:

Warning: Invalid callback PDOSession::_destroy, cannot access protected method PDOSession::_destroy() in auth.php on line 49

Warning: session_destroy() [function.session-destroy]: Session object destruction failed in auth.php on line 49

Warning: Invalid callback PDOSession::_close, cannot access protected method PDOSession::_close() in auth.php on line 49

为什么我会收到错误?如果我公开这些方法,那么我会收到有关访问$this

的错误

1 个答案:

答案 0 :(得分:1)

session_set_save_handler(array(__CLASS__, '_open'),
                         array(__CLASS__, '_close'),
                         array(__CLASS__, '_read'),
                         array(__CLASS__, '_write'),
                         array(__CLASS__, '_destroy'),
                         array(__CLASS__, '_gc'));

这些已注册的方法需要从外部访问,所以你必须将它们声明为public,我认为你必须使用$ this关键字而不是 CLASS constans因为这些方法不是静态的。< / p>