middlewares/php-session

使用请求数据启动php会话的中间件

v3.1.1 2022-03-13 01:22 UTC

This package is auto-updated.

Last update: 2024-09-13 06:55:01 UTC


README

Latest Version on Packagist Software License Testing Total Downloads

使用请求数据启动php会话并在返回响应后关闭它。在PSR-7请求/响应中读取和写入会话cookie。

要求

安装

此软件包可以通过Composer安装和自动加载,作为middlewares/php-session

composer require middlewares/php-session

示例

Dispatcher::run([
	new Middlewares\PhpSession(),

    function () {
        //Use the global $_SESSION variable to get/set data
        $_SESSION['name'] = 'John';
    }
]);

用法

这是一个中间件,使用服务器请求的cookie启动原生的PHP会话。

name

会话名称。如果没有提供,则使用PHP的默认名称(PHPSESSID)。更多信息请参阅session_name

// Start the session with other name
$session = (new Middlewares\PhpSession())->name('user_session');

id

此选项设置会话ID。如果没有提供,则使用请求的cookie来获取它。

// Start the session with a specific session id
$session = (new Middlewares\PhpSession())->id('foo');

options

这允许设置传递给session_start()的选项

// Start the session with a specific session id
$session = (new Middlewares\PhpSession())->options([
    'cookie_lifetime' => 86400
]);

regenerateId

此选项在特定时间间隔后重新生成ID。最新重新生成时间保存在键session-id-expires中,但您可以在第二个参数中更改它

// Regenerate the session id after 60 seconds
$session = (new Middlewares\PhpSession())->regenerateId(60);

// Regenerate the session id after 60 seconds, storing the expires date in the key 'expiresAt'
$session = (new Middlewares\PhpSession())->regenerateId(60, 'expiresAt');

有关最近更改的更多信息,请参阅变更日志,有关贡献细节请参阅贡献

MIT许可证(MIT)。更多信息请参阅许可证