pitch7900/phpsqlsessionhandler

PHP 的 SQL 会话处理程序

dev-main 2023-08-12 15:46 UTC

This package is auto-updated.

Last update: 2024-09-12 18:07:02 UTC


README

此库允许在数据库中处理 PHP 会话。这主要用于经过身份验证的会话以及维护多个 PHP 服务器实例之间的会话。它还允许在会话启动后同时处理多个 HTTP 请求。

先决条件

Composer 要求

"php": "^7.3|^7.4|^8",
"illuminate/database": "^6|^7|^8|^9",
"symfony/var-dumper": "^5|^6"

数据库

要使此代码运行,至少需要有一个名为 "sessions" 的表,如下所示

--
-- Table structure for table `sessions`
--

DROP TABLE IF EXISTS `sessions`;
CREATE TABLE IF NOT EXISTS `sessions` (
  `id` varchar(32) NOT NULL,
  `timestamp` int(10) UNSIGNED DEFAULT NULL,
  `data` mediumtext,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `timestamp` (`timestamp`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

在使用 Illuminate capsule 管理器加载会话之前,应在您的应用程序中声明数据库

PHP 中的代码声明(示例)

这是调用此函数的方式

use Pitch7900\SessionsHandler\DBSessionsHandler;

session_cache_limiter('public');
ini_set("session.cookie_httponly", 1);
session_name('APPS_SESSID');

//Use a custom SessionHandler Based on database.
$handler = new DBSessionsHandler(3600,'user',$rootPath."/logs/sessions.log",false);
session_set_save_handler($handler, true);
session_start();

DBSessionsHadnlder 声明

默认情况下,未经身份验证的会话将在空闲 30 秒后过期。

Pitch7900\SessionsHandler\DBSessionsHandler::__construct

__construct

<?php
public function __construct(
    int $session_duration = 3600,
    $authenticatedUserValue = null,
    ?string $logfile = null,
    bool $debug = false
) { }
@param int $session_duration : duration in seconds for an authenticated session

@param string $logfile : logfile for debug

@param bool $debug : enable debug mode 

@param mixed|null $authenticatedUserValue : String Value to lookup for an authenticated user. This can be changed depending on how you authenticate your sessions

@return void

实现示例

可以在以下 Slim 模板中找到实现示例: https://github.com/pitch7900/slim4Template 请参阅 bootstrap/app.php 文件。