xp-forge/sessions

v3.2.0 2024-03-24 13:25 UTC

README

Build status on GitHub XP Framework Module BSD Licence Requires PHP 7.0+ Supports PHP 8.0+ Latest Stable Version

示例

use web\session\{InFileSystem, ForTesting};

// Instantiate session factory
$sessions= new InFileSystem('/tmp');
$sessions= (new ForTesting())->lasting(3600)->named('psessionid');

// Create a new session
$session= $sessions->create();

// Open an existing session...
if ($session= $sessions->open($sessionId)) { … }

// ...or locate session attached to a request
if ($session= $sessions->locate($request)) { … }

// Basic I/O operations
$session->register('key', 'value');
$value= $session->value('key');
$keys= $session->keys();
$session->remove('key');

// Destroy
$session->destroy();

// Close session...
$session->close();

// ...or close and then transmit session to response.
$session->transmit($response);

确保始终调用 close()transmit() 以同步会话数据。

实现

该库包含以下实现

  • web.session.InFileSystem - 使用本地文件系统进行序列化数据
  • web.session.ForTesting - 内部会话,用于测试目的

其他实现提供集群解决方案

安全

所有会话cookie已设置安全标志。如果您仅使用http在localhost上进行开发,您需要如下告知会话实例

// This will omit the "Secure" flag from session cookies in dev environment
$sessions= new InFileSystem('/tmp');
if ('dev' === $this->environment->profile()) {
  $sessions->cookies()->insecure(true);
}