laikait/laika-session
Composer 安装命令:
composer require laikait/laika-session
包简介
A php singleton file,pdo,redis or memcached session handler.
README 文档
README
A PHP session package for the Laika Framework supporting File, PDO, Redis, and Memcached backends via a clean static facade.
Requirements
- PHP
>= 8.1 ext-pdo— for PDO driverext-redis— for Redis driverext-memcached— for Memcached driver
Installation
composer require laikait/laika-session
Quick Start
Call SessionManager::config() once at your application bootstrap, before any session reads or writes.
use Laika\Session\SessionManager; use Laika\Session\Session; // File driver (default — no instance required) SessionManager::config(); // Write and read Session::set('user_id', 42); echo Session::get('user_id'); // 42
Drivers
File
Stores sessions as files on disk. No dependencies. Suitable for single-server deployments.
SessionManager::config(null, [ 'path' => '/var/www/storage/sessions', // optional, defaults to session_save_path() 'prefix' => 'LK', // optional, default 'LK' ]);
PDO (MySQL)
Stores sessions in a database table. Pass a pre-configured PDO instance. The sessions table is created automatically on first use.
$pdo = new PDO( 'mysql:host=127.0.0.1;dbname=myapp;charset=utf8mb4', 'username', 'password', [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, ] ); SessionManager::config($pdo);
Auto-created table schema:
CREATE TABLE IF NOT EXISTS `sessions` ( `id` VARCHAR(128) PRIMARY KEY, `data` BLOB, `last_activity` INT );
Redis
Pass a connected and authenticated Redis instance.
$redis = new Redis(); $redis->connect('127.0.0.1', 6379); $redis->auth('your-password'); // omit if no auth SessionManager::config($redis, [ 'prefix' => 'LK', // optional, default 'LK' 'gc_maxlifetime' => 1440, // optional, seconds — defaults to session.gc_maxlifetime ini ]);
Memcached
Pass a configured Memcached instance.
$memcached = new Memcached(); $memcached->addServer('127.0.0.1', 11211); SessionManager::config($memcached, [ 'prefix' => 'LK', // optional, default 'LK' 'gc_maxlifetime' => 1440, // optional, seconds — defaults to session.gc_maxlifetime ini ]);
Configuration
Session Options
Override PHP session options after calling config():
SessionManager::setOptions([ 'name' => 'MY_APP', // session cookie name, default 'LAIKA' 'gc_maxlifetime' => 3600, // session lifetime in seconds, default 1440 'gc_probability' => 1, 'gc_divisor' => 100, ]);
Cookie Parameters
SessionManager::setCookies([ 'path' => '/', 'domain' => '.example.com', 'secure' => true, // HTTPS only — default true 'httponly' => true, // no JS access — default true 'samesite' => 'Strict', // default 'Strict' ]);
Default cookie parameters:
| Parameter | Default |
|---|---|
path |
/ |
secure |
true |
httponly |
true |
samesite |
Strict |
Session API
All methods are static and available on the Session facade.
Session::set()
Store one or multiple values. Data is namespaced under a $for key (default APP).
// Single value Session::set('user_id', 42); // Multiple values at once Session::set(['user_id' => 42, 'role' => 'admin']); // Custom namespace Session::set('token', 'abc123', 'AUTH');
Session::get()
Retrieve a value. Returns null if not found.
$userId = Session::get('user_id'); // from 'APP' namespace $token = Session::get('token', 'AUTH'); // from 'AUTH' namespace
Session::has()
Check if a key exists.
if (Session::has('user_id')) { // logged in }
Session::pop()
Remove a key if it exists.
Session::pop('flash_message'); Session::pop('token', 'AUTH');
Session::all()
Return the entire $_SESSION superglobal.
$all = Session::all();
Session::regenerate()
Regenerate the session ID. Pass false to keep the old session data.
Session::regenerate(); // regenerate and delete old session Session::regenerate(false); // regenerate but keep old session data
Session::id()
Get the current session ID.
$id = Session::id();
Session::name()
Get the current session name.
$name = Session::name();
Session::end()
Destroy the session and all its data.
Session::end();
Namespacing
Sessions are stored under a namespace key ($for) within $_SESSION. This prevents key collisions when multiple parts of your application share a session.
Session::set('id', 42, 'USER'); Session::set('id', 99, 'CART'); Session::get('id', 'USER'); // 42 Session::get('id', 'CART'); // 99
The default namespace is APP.
Full Bootstrap Example
use Laika\Session\SessionManager; use Laika\Session\Session; // 1. Configure driver $pdo = new PDO('mysql:host=127.0.0.1;dbname=myapp', 'user', 'pass'); SessionManager::config($pdo); // 2. Customise options (optional) SessionManager::setOptions(['name' => 'MY_APP', 'gc_maxlifetime' => 7200]); SessionManager::setCookies(['domain' => '.example.com']); // 3. Use the Session facade anywhere Session::set('user_id', 1); if (Session::has('user_id')) { $id = Session::get('user_id'); Session::regenerate(); // rotate session ID on privilege change } // On logout Session::end();
License
MIT — see LICENSE for full terms.
laikait/laika-session 适用场景与选型建议
laikait/laika-session 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.3k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 11 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「redis」 「php」 「pdo」 「memcached」 「session」 「singleton」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 laikait/laika-session 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 laikait/laika-session 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 laikait/laika-session 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Microservice RPC through message queues.
The CodeIgniter Redis package
贝嘟分布式缓存扩展
Redis distributed locks for laravel
Redis backed library for creating background jobs and processing them later.
统计信息
- 总下载量: 2.3k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 32
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-26