1

我正在尝试在 PHP 中实现一个自定义 SessionHandler,它使用事务通过数据库处理会话。

但问题是我不断收到致命错误,关于该函数为空。

致命错误:在 null 上调用成员函数 begin_transaction()
致命错误:在 null 上调用成员函数 commit()

如果我注释掉前两个,我会得到:
致命错误:在 null 上调用成员函数 prepare()
致命错误:在 null 上调用成员函数 close()

等等等等。

现在显然,您会认为我忘记启动与数据库的连接。好吧,令人惊讶的是,我没有。

<?php
    if (!empty($_POST)) {
        $username = trim($_POST['username']);
        $password = trim($_POST['password']);

        require_once '../psl-config.php';
        $db = new mysqli('localhost', USER, PASSWORD, DATABASE, 0, '/var/run/mysqld/mysqld.sock');

        if (!$db->connect_errno) {
            $login = $db->prepare('SELECT hashedPassword FROM gebruikerGegevens WHERE username = ?');
            $login->bind_param('s', $username);
            $login->execute();
            $login->bind_result($hashedPassword);
            $login->fetch();
        }

        if (password_verify($password, $hashedPassword)) {
            require_once 'sessionHandler.php';

            $handler = new MysqlSessionHandler($db);
            session_set_save_handler($handler);
            session_start();
        } else {
            echo "Username or password is incorrect.";
        }
    }
?>

在我启动 MysqlSessionHandler 函数之前,一切都很顺利。 注意:我还没有插入回滚功能,因为我仍然忙于找出错误。

<?php
class MysqlSessionHandler implements SessionHandlerInterface {
    protected $db;

    public function open($save_path, $session_name) {
        return true;
    }

    public function read($session_id) {
        $db->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
        $db->prepare('SELECT expiry, data FROM sessions WHERE sid = ?');
        $db->bind_param('s', $session_id);
        $db->execute();
        $db->bind_result($expiry, $data);
        $result = $mysqli->fetch();

        if ($result) {
            if ($expiry < time()) {
                return '';
            }
            return $data;
        }

        //Inserts sessionID into database, if no sessionID could be found in the database.
        $db->prepare('INSERT INTO sessions (sid, expiry, data) VALUES (?,?,?)');
        $db->bind_param('sib', $session_id, $create_expiry, $empty_data);
        $create_expiry = session_cache_expire();
        $empty_data = '';
        $db->execute;
        return '';
    }

    public function write($session_id, $session_data) {
        $db->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
        $db->prepare('INSERT INTO sessions (sid, expiry, data) VALUES (?,?,?)');
        $db->bind_param('sib', $session_id, $create_expiry, $session_data);
        $create_expiry = session_cache_expire();
        $db->execute();
        return true;
    }

    public function close() {
        $db->commit();
        $db->close();
        return true;
    }

    public function  destroy($session_id) {
        $db->prepare('DELETE FROM sessions WHERE sid = ?');
        $db->bind_param('s', $session_id);
        $db->execute();
        return true;
    }

    public function gc($maxlifetime) {
        return true;
    }
}
?>
4

1 回答 1

2

$db变量是一个实例变量,这意味着它必须使用$this.

例子

$this->db->begin_transaction(...);

但在能够以$db这种方式引用之前,您必须使用构造函数方法对其进行设置。

class MysqlSessionHandler implements SessionHandlerInterface {
    protected $db;

    public function __construct($db) {
        $this->db = $db;
    }

    public function read(...) {
        ...
        $this->db->begin_transaction(...);
        $this->db->prepare(...);
        ...
    }
}
于 2015-12-26T01:08:59.463 回答