genai/session
Composer 安装命令:
composer require genai/session
包简介
Session component with pluggable storage: native files (default), a project file dir (cache/session), or a database (PDO). A small Session API over native PHP sessions; a SessionStore save-handler decides where the data lives. Driver chosen from the [session] config group. PHP 5.3-safe runtime.
README 文档
README
A session component with pluggable storage. A small Session API over native
PHP sessions; a SessionStore save-handler decides where the data lives:
| driver | store | use it for |
|---|---|---|
default |
native files | single server, simplest |
file |
FileStore |
project-local files (cache/session/) |
database |
PdoStore |
multiple servers (shared sessions table) |
The cookie, session id, and GC stay native — only the read/write target changes.
API
$session->set('uid', 7); $uid = $session->get('uid', 0); $session->has('uid'); $session->remove('uid'); $session->pull('once'); $session->flash('msg', 'Saved!'); // available on the NEXT request $msg = $session->getFlash('msg'); $session->regenerate(); // on login (anti-fixation) $session->destroy(); // on logout
Starts lazily on first use, so responses that never touch the session set no cookie.
Config ([session] group, optional)
[session] driver = file ; default | file | database name = KIDSAFE lifetime = 0 ; cookie seconds (0 = until browser close) path = cache/session ; driver=file table = sessions ; driver=database
Wiring (one bean)
The factory picks the store from config; the app decides whether it has a PDO to pass:
#[Configuration] class SessionConfig { #[Bean(\GenAI\Session\Session::class)] public function session(\GenAI\Session\Bundle\SessionProperty $cfg, \PDO $pdo) { return \GenAI\Session\SessionFactory::build($cfg, $pdo); // omit $pdo if no DB } }
Then inject GenAI\Session\Session anywhere and drop manual session_start().
Notes
SessionPropertyauto-registers (the package declaresextra.genai.scan); justrequire genai/session.- PHP 5.3 has no
SessionHandlerInterface, so this uses the 6-callback save-handler form plus asession_write_closeshutdown hook (so custom stores flush before objects are destroyed). - SameSite isn't set on the cookie (needs PHP 7.3+); httponly is always on, secure is auto on HTTPS.
databaseneedsext-pdo. The table is created on first use;UPDATE-then-INSERTkeeps it SQLite/MySQL-portable.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 2
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2026-07-10