sylae/doctrine-dbal-session-handler 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sylae/doctrine-dbal-session-handler

最新稳定版本:v0.2

Composer 安装命令:

composer require sylae/doctrine-dbal-session-handler

包简介

SessionHandlerInterface implementation using doctrine/dbal

README 文档

README

This provides a single class, \Doctrine\DBAL\DBALSessionHandler, which allows you to toss your session onto an SQL database with as little effort as possible.

As of now, it's only tested on mysql8, but it should work fine for anything really? The only sticking point is a REPLACE INTO command, but that can easily be pull-requested :p

Example of use:

$db = \Doctrine\DBAL\DriverManager::getConnection(/* or whatever */);

$sessionHandler = new \Doctrine\DBAL\DBALSessionHandler($db);
$sessionHandler->setSessionTable("whatever"); // defaults to "sessions" if not called
session_set_save_handler($sessionHandler, true);

By default, we also store the IP and user agent of these sessions alongside the actual session data. In addition, you can provide a user ID by passing along a callable. This is to facilite allowing the user to see what sessions they have active on their account, and revoking them through your site.

$sessionHandler->setUserIDHandler(function (): ?int {
    return $this->user->id ?? null; // or wherever you keep your user ID.
});

Right now it doesnt do any bougie-ass table management for you, so you have to do a little bit of sql:

/* minimum you can get away with */
CREATE TABLE `sessions`
(
    `idSession` char(64) NOT NULL,
    `data`      text,
    `ip`        binary(16)      DEFAULT NULL,
    `userAgent` varchar(255)    DEFAULT NULL,
    `idUser`    bigint unsigned DEFAULT NULL, /* change size as needed, this is just what i use */
    PRIMARY KEY (`idSession`),
);

/* what i use */
CREATE TABLE `sessions`
(
    `idSession` char(64) NOT NULL,
    `updated`   datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `created`   datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `data`      text,
    `ip`        binary(16)        DEFAULT NULL,
    `userAgent` varchar(255)      DEFAULT NULL,
    `idUser`    bigint unsigned   DEFAULT NULL,
    PRIMARY KEY (`idSession`),
    KEY `sessions_ibfk_1` (`idUser`),
    CONSTRAINT `sessions_ibfk_1` FOREIGN KEY (`idUser`) REFERENCES `users` (`idUser`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_0900_ai_ci;

统计信息

  • 总下载量: 32
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-08-16