middlewares / php-session
使用请求数据启动php会话的中间件
v3.1.1
2022-03-13 01:22 UTC
Requires
- php: ^7.2 || ^8.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- laminas/laminas-diactoros: ^2.3
- middlewares/utils: ^3.0
- oscarotero/php-cs-fixer-config: ^1.0
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^8|^9
- squizlabs/php_codesniffer: ^3.0
README
使用请求数据启动php会话并在返回响应后关闭它。在PSR-7请求/响应中读取和写入会话cookie。
要求
- PHP >= 7.2
- PSR-7 http消息实现
- PSR-15中间件分发器
安装
此软件包可以通过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)。更多信息请参阅许可证