programster/mysql-session-handler

MySQL PHP 会话处理器

1.0.2 2020-12-05 17:10 UTC

This package is auto-updated.

Last update: 2024-09-08 14:21:45 UTC


README

此仓库包含一个使用 MySQL 作为后端的自定义 PHP 会话处理器。还有一个使用此包的示例代码库

安装方法

使用 Composer

如果您还没有安装 composer,请安装它(Linux 指令)。

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

composer require "programster/mysql-session-handler"

使用方法

您可能只需观看这个 YouTube 展示视频

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

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 \Programster\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 公共许可证