svk27/mysql-session-handler

MySQL PHP Session Handler - 自定义使用 - SekureSessions

1.1.1 2021-06-03 15:07 UTC

This package is auto-updated.

Last update: 2024-09-29 05:49:08 UTC


README

此存储库包含一个使用MySQL作为后端的自定义PHP会话处理器。

如何安装

使用 Composer

如果您尚未安装,请安装composer。

导航到您的项目目录,并运行以下命令

composer require "svk27/mysql-session-handler"

如何使用

以下是一个演示如何使用此工具的脚本。

require 'vendor/autoload.php';

# Create your MySQL database connection
$db = new mysqli('localhost', 'username', 'password', 'database');

# Create the session handler using that connection and pass it the name of the table
# The handler will try to create it if it doesn't already exist.
$handler = new \SekureSessions\SessionHandler\SessionHandler($db, 'my_sessions_table');

# Tell PHP to use the handler we just created.
session_set_save_handler($handler, true);

# Start your session
session_start();

# Set a session variable.
$_SESSION['my_session_variable'] = 'some data here';

手动创建会话表

如果会话表不存在,该工具将尝试使用数据库连接来创建它。如果您提供的处理程序用户凭据没有创建表的权限,您可以使用以下SQL语句手动创建表

CREATE TABLE IF NOT EXISTS `sessions` (
    `id` varchar(32) NOT NULL,
    `modified_timestamp` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `data` mediumtext,
    PRIMARY KEY (`id`),
    KEY `modified_timestamp` (`modified_timestamp`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

作者

许可证

MIT公共许可证