简体   繁体   中英

php session class using PDO - getting errors

My session class:

<?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();

My Errors:

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

Why am I getting the errors? If I make the methods public, then I get errors about accessing $this .

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

These registered methods needs to be accessible from outside so you have to declare them as public and i think you have to use $this keyword instead of CLASS constans becouse these methods are't static.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM