kooser / session
安全地管理和保存会话数据。
v5.0.1
2020-09-12 12:10 UTC
Requires
- php: >=7.3
- cakephp/database: ^4.1
- defuse/php-encryption: ^2.2
- symfony/cache: ^5.1
- symfony/filesystem: ^5.1
- symfony/options-resolver: ^5.1
- symfony/polyfill-apcu: ^1.18
- symfony/polyfill-ctype: ^1.18
Requires (Dev)
- phpunit/phpunit: ^9
This package is auto-updated.
Last update: 2024-09-07 12:47:00 UTC
README
安装
安装Session Lock的最佳方式是通过composer。如果您还没有安装composer,可以直接从composer网站安装。成功安装composer后,运行以下命令行代码。
composer require omatamix/session-lock
使用方法
会话管理器
会话管理器提供了一个简单的API。
use Omatamix\SessionLock\SessionManager; // Construct a new session manager. $session = new SessionManager();
开始或恢复会话。
$session->start();
检查我们的会话是否正在运行。
if ($session->exists()) { echo "The session is running!"; }
put方法设置会话变量。
$session->put('hello', 'world');
检查此会话变量是否已设置。
if ($session->has('hello')) { echo "The session variable exists."; }
delete方法删除会话变量。
$session->delete('hello');
get方法获取会话变量的值。
echo "Hello " . $session->get('hello') . "!";
flash方法与get相同,但flash将在检索后会话变量。
echo "Hello " . $session->flash('hello') . "!";
停止会话。
$session->stop();
会话再生
用新生成的ID更新当前会话ID非常简单。
$session->regerate();
会话指纹
此会话管理器内置会话指纹功能,以某种方式提高会话安全性。当您创建会话处理器实例时,默认启用会话指纹,它会绑定您的远程IP和用户代理。如果您不想启用此功能,可以使用以下方法关闭。
$session = new SessionManager([ 'fingerprinting' => false, ]);
您还可以像这样禁用绑定远程IP或用户代理。
$session = new SessionManager([ 'bind_ip_address' => false, // If set to true we will bind the ip address else dont. 'bind_user_agent' => false, // If set to true we will bind the user agent else dont. ]);
如果您使用的是受信任的代理,可以使用以下方法设置远程IP。
$session = new SessionManager([ 'use_ip' => '127.0.0.1', ]);
会话处理器
您还可以使用会话处理器设置会话信息如何存储。
use Omatamix\SessionLock\SessionHandlers\CacheSessionHandler; $session = new SessionManager(); $session->setSaveHandler(new CacheSessionHandler(/** A `psr/cache` or `psr/simple-cache` pool. */));
支持的
Omatamix\SessionLock\SessionHandlers\CacheSessionHandler::class
Omatamix\SessionLock\SessionHandlers\CookieSessionHandler::class
Omatamix\SessionLock\SessionHandlers\DatabaseSessionHandler::class
Omatamix\SessionLock\SessionHandlers\NullSessionHandler::class
加密适配器
此库还包括加密会话处理器。
use Defuse\Crypto\Key; use Omatamix\SessionLock\Encryption\Adapter\Defuse; use Omatamix\SessionLock\Encryption\Encrypted; $session = new SessionManager(); $session->setSaveHandler(new Encrypeted(new CacheSessionHandler(/** A `psr/cache` or `psr/simple-cache` pool. */), new Defuse(Key::createNewRandomKey())); // All session data will now be encrpyted using the `defuse` adapter.
支持的
Omatamix\SessionLock\Encryption\Adapter\Defuse::class
Omatamix\SessionLock\Encryption\Adapter\Halite::class
会话配置
您还可以通过会话管理器构造函数方法传递会话配置。
$session = new SessionManager([ 'config' => [ 'use_cookies' => true, 'use_only_cookies' => true, 'cookie_httponly' => true, 'cookie_samesite' => 'Lax', 'use_strict_mode' => true, ] ]);
安全漏洞
如果您在Session Lock中发现安全漏洞,请通过omatamix@gmail.com发送电子邮件给Nicholas。所有安全漏洞都将得到及时处理。
贡献
所有贡献都受欢迎!如果您愿意贡献。
许可证
本项目采用MIT许可证条款。