2

我正在构建一个需要包含用户身份验证的数据库驱动网站。如果我使用标准的基于文件的会话管理,一切都按预期工作,但如果我尝试使用自定义会话处理程序(以便将会话保存在数据库中),似乎唯一存储的东西是 ID 和时间戳。

保存在变量中的任何内容都是 nuked (尽管有趣的是,只有在成功登录后我使用重定向到另一个页面时$_SESSION才会发生这种情况。header(Location: ...)

这是我的会话处理程序类文件:

// include class files
require_once('database.php');
require_once('database-functions.php');

// set up session handler class
class Session {

    private $session_login  = null;
    private $session_db         = null;

    public function __construct() {

        // connect to database
        $this -> session_login  = new Database;
        $this -> session_db         = new SimplifiedDB;

        $session_db_info = $this -> session_login -> getLogin();

        $this -> session_db -> dbConnect(
            $session_db_info['host'],
            $session_db_info['user'],
            $session_db_info['pass'],
            $session_db_info['database']);

    }

    // open
    public function open($save_path, $session_name) {

        return true;

    }

    // close
    public function close() {

        return true;

    }

    // read
    public function read($id) {

        $get_data = $this -> session_db -> dbGetVariable('session', 'data', array('id' => $id));

        if(empty($get_data)) {

            return '';

        } else {

            return $get_data['data'];

        }

    }

    // write
    public function write($id, $data) {

        $access = time();

        $get_data = $this -> session_db -> dbGetVariable('session', 'id', array('id' => $id));

        if(empty($get_data)) {

            // no ID, insert a record
            $write_query = $this -> session_db -> dbInsert('session', array(
                'id'        => $id,
                'data'      => $data,
                'access'    => $access));

            return true;

        } else {

            // ID exists, update it
            $write_query = $this -> session_db -> dbUpdate('session', array(
                'id'        => $id,
                'data'      => $data,
                'access'    => $access));

            return true;

        }

        return false;

    }

    // destroy (destroys a session)
    public function destroy($sessionId) {

        $destroy_query = $this -> session_db -> dbDelete('session', array('id'  => $sessionId));

        return true;

    }


    // garbage collector (randomly deletes old sessions)
    public function gc($lifetime) {

        $the_time = time();

        $sql = "DELETE FROM 'session' WHERE 'access' + $lifetime < $this_time";
        $gc_query = $this -> session_db -> dbExecuteQuery($sql);

        return true;

    }

}

我正在使用SimplifiedDB类 ( database-functions.php) 使用 PDO 连接到 MySQL 数据库,该database.php文件是另一个包含我的数据库凭据的类。

我在这个问题上花了很多时间,我一生都无法弄清楚我哪里出错了。我有一个header.php文件,我包含在每个 PHP 文件的第一行,内容如下:

require_once('sessions.php');

$user_session = new Session();

session_set_save_handler(
    array($user_session, 'open'),
    array($user_session, 'close'),
    array($user_session, 'read'),
    array($user_session, 'write'),
    array($user_session, 'destroy'),
    array($user_session, 'gc'));

register_shutdown_function('session_write_close');

session_start();

我最初确实在我的会话处理程序类的 Session 构造函数中有这些东西,但最后我把它放在了一个单独的类中(可能是出于绝望)。我的虚拟主机说没有关于会话管理的服务器问题,而且我的本地主机网络服务器上也有这个问题,所以代码肯定有问题。

我还应该指出,我已经进行了一些其他检查,例如确保我的header.php文件位于每个 PHP 文件的第一行,并且我exit()在所有重定向之后都使用,如下所示:

$_SESSION['test'] = 'testval';
header("Location: /index.php");
exit();

(顺便说一句,在上面的标题行中使用完整的 URL 没有区别,我仍然有消失的会话变量)。

感谢您花时间阅读本文,希望有人能够指出我错过的任何内容?:)

4

0 回答 0