inanepain/session
Composer 安装命令:
composer require inanepain/session
包简介
A lightweight, secure and extensible PHP session handling library.
README 文档
README
Table of Contents
inanepain/session- 1. Introduction
- 2. Install
- 3. Quick Start
- 4. Configuration
- 5. API Reference
- 6. Examples
- 7. Security Considerations
inanepain/session
A lightweight, secure and extensible PHP session handling library.
1. Introduction
SessionManager is a lightweight, secure, and extensible PHP session handling library designed for modern web applications. It provides a simple static API for managing sessions with built-in security features like HTTP-only cookies, SameSite protection, periodic ID regeneration, and inactivity timeouts. It supports namespacing to avoid key collisions and "remember me" functionality for persistent sessions across browser closes.
Key features: * Automatic secure session initialization. * Namespaced storage to organize data (e.g., user, cart). * Flash messages for one-time notifications. * Configurable timeouts and regeneration intervals. * Memory-safe handling for large sessions. * Persistent sessions via "remember me" (30-day default).
This library is dependency-free, PSR-compliant, and production-ready.
1.1. Why SessionManager?
Native PHP sessions are powerful but lack secure defaults and organization.
SessionManager wraps session_* functions with best practices, preventing common pitfalls like fixation attacks and key conflicts.
2. Install
composercomposer require inanepain/session
3. Quick Start
3.1. Basic Usage
Require the file and initialize:
SessionManager::init([ 'name' => 'MYAPP_SESSID', // 'cookie_secure' => true, // HTTPS only 'cookie_samesite' => 'Strict', ]); // Set and get data SessionManager::set('user_id', 123); echo SessionManager::get('user_id'); // 123 // Flash message SessionManager::flash('success', 'Login successful!'); header('Location: /dashboard'); exit; // In dashboard.php if (SessionManager::hasFlash('success')) { echo SessionManager::getFlash('success'); } // Logout SessionManager::destroy();
3.2. Namespaced Sessions
Use the base class SessionNamespace for modular access:
class UserSession extends SessionNamespace { protected const NAMESPACE = 'user'; } class CartSession extends SessionNamespace { protected const NAMESPACE = 'cart'; } // Usage UserSession::set('id', 456); CartSession::set('items', ['item1']); echo UserSession::get('id'); // 456
4. Configuration
Pass options to init() to customize behaviour. Defaults are secure.
| Option | Type | Default | Description |
|---|---|---|---|
cookie_lifetime |
int |
0 |
Cookie expiry (0 = browser close; >0 for persistent). |
cookie_path |
string |
/ |
Cookie path. |
cookie_domain |
string |
'' |
Cookie domain. |
cookie_secure |
bool |
HTTPS detected |
HTTPS-only cookie. |
cookie_httponly |
bool |
true |
Prevent JS access. |
cookie_samesite |
string |
Lax |
CSRF protection (Lax/Strict/None). |
use_strict_mode |
bool |
true |
Reject uninit sessions. |
use_only_cookies |
bool |
true |
No URL param fallback. |
name |
string |
PHPSESSID |
Session cookie name. |
gc_maxlifetime |
int |
1440 |
Garbage collection (minutes). |
memory_limit |
string |
null |
Temp boost (e.g., '2G'; dev only). |
max_session_size |
int |
104857600 |
Clear if >100MB (bytes). |
force_clear |
bool |
false |
Nuke session on init (dev). |
remember_me |
bool |
false |
Auto-set lifetime to 30 days. |
Example with persistent session:
SessionManager::init([ 'remember_me' => true, // 30-day cookie // 'cookie_secure' => true, ]);
Runtime toggles:
SessionManager::enableRememberMe(86400 * 7); // 1 week SessionManager::disableRememberMe(); // Back to session-only echo SessionManager::isRememberMe() ? 'Persistent' : 'Temporary';
5. API Reference
All methods are static. Call init() first.
5.1. Initialization
Method |
Description |
|
Start session with config. Idempotent. |
5.2. Core Storage
Method |
Description |
|
Store value; updates activity. |
|
Retrieve value. |
|
Key exists? |
|
Remove key. |
|
All namespace data. |
|
Empty current namespace. |
5.3. Namespacing
Method |
Description |
|
Switch namespace. |
|
Active namespace. |
5.4. Flash Messages
Method |
Description |
|
Set for next request. |
|
Get and erase. |
|
Exists? (non-destructive). |
5.5. Security & Lifecycle
Method |
Description |
|
New ID; updates activity. |
|
Interval (min 60s). |
|
Inactivity timeout (min 1s). |
|
Full logout (clears data/cookie). |
|
Raw session ID. |
5.6. Remember Me
Method |
Description |
|
Enable persistent (regenerates ID). |
|
Disable (session-only). |
|
Currently persistent? |
6. Examples
6.1. Login with Remember Me
SessionManager::init(['remember_me' => $_POST['remember'] ?? false]); if (authenticate($_POST['email'], $_POST['password'])) { SessionManager::set('user_id', $user->id); SessionManager::enableRememberMe(); // If checkbox checked SessionManager::flash('success', 'Welcome!'); header('Location: /dashboard'); } else { SessionManager::flash('error', 'Invalid credentials'); }
6.2. Namespaced E-Commerce
UserSession::set('logged_in', true); CartSession::set('total', 99.99); echo CartSession::all(); // ['total' => 99.99] SessionManager::namespace('user'); // Switch back
7. Security Considerations
-
Defaults: HTTP-only, SameSite=Lax, strict mode enabled.
-
Regeneration: Auto every 10min; manual via
regenerate(). -
Timeouts: 30min inactivity → auto-destroy.
-
Persistent Sessions: Use secure cookies; validate user on resume (e.g., DB token).
-
Large Data: Avoid storing files/objects; use DB/Redis for >1MB.
-
File Handler: Auto-clears oversized/corrupt sessions; consider Redis for scale.
inanepain/session 适用场景与选型建议
inanepain/session 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 11 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「library」 「session」 「secure」 「remember me」 「inane」 「inanepain」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 inanepain/session 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 inanepain/session 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 inanepain/session 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Secure Trading driver for the Omnipay PHP payment processing library
Secure session middleware for Slim 3 framework
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
Non-I/O blocking PHP SessionHandler implementation using Nette/Caching with DI Extension for Nette framework
Inbox pattern process implementation for your Laravel Applications
Add a secure password generator to Signup Modal
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 37
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unlicense
- 更新时间: 2025-11-24