xp-forge / sessions
会话
v3.2.0
2024-03-24 13:25 UTC
Requires
- php: >=7.0.0
- xp-forge/web: ^4.0 | ^3.0 | ^2.0 | ^1.0
- xp-framework/core: ^12.0 | ^11.0 | ^10.0 | ^9.0 | ^8.0 | ^7.3
Requires (Dev)
- xp-framework/test: ^2.0 | ^1.0
README
示例
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
- 内部会话,用于测试目的
其他实现提供集群解决方案
- https://github.com/xp-forge/redis-sessions
- https://github.com/xp-forge/mongo-sessions
- https://github.com/xp-forge/cookie-sessions
安全
所有会话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); }