omatamix/session-lock

安全管理和保留会话数据。

v5.0.1 2020-09-12 12:10 UTC

README

Continuous Integration

安装

安装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许可证的条款进行许可。