claytonpulman/zebra_session_plus
Composer 安装命令:
composer require claytonpulman/zebra_session_plus
包简介
A drop-in replacement for PHP's default session handler which stores session data in a MySQL database, providing better performance, better security and protection against session fixation and session hijacking - now with redis cache support
README 文档
README
A drop-in replacement for PHP's default session handler which stores session data in a MySQL database, enhanced with optional Redis caching for high performance.
Overview
Zebra Session replaces PHP's default file-based session handling with a database-backed solution, improving performance, security, and scalability.
This version extends the original Zebra_Session by adding Redis as an optional cache layer, without breaking existing behavior.
- MySQL = Source of Truth
- Redis = Cache (optional)
✨ Features
- MySQL-backed PHP sessions
- Optional Redis caching
- Protection against session hijacking & fixation
- Session locking for concurrent AJAX requests
- Flash session data
- Garbage collection
- Works with mysqli & PDO
- Drop-in replacement
📦 Requirements
- PHP 8.0+
- MySQL 5.7+ / MariaDB
- Redis 6+ (optional)
- Composer
mysqliorPDOextension
📂 Project Structure
project-root/
│
├── install/
│ └── session_data.sql
├── vendor/
│ └── autoload.php
├── examples/
│ └── example.php
├── README.md
├── Zebra_Session.php
🗄️ Database Setup
Create Database
CREATE DATABASE zebra-session;
Create Session Table
Import install/session_data.sql or run:
CREATE TABLE `session_data` (
`session_id` varchar(128) NOT NULL,
`hash` varchar(32) NOT NULL,
`session_data` longblob NOT NULL,
`session_expire` int(11) NOT NULL,
PRIMARY KEY (`session_id`),
KEY `session_expire` (`session_expire`)
) ENGINE=InnoDB;
📥 Installation
Install via Composer
composer require stefangabos/zebra_session
composer require predis/predis
Manual Install
require 'path/to/Zebra_Session.php';
🚀 Basic Usage
$link = mysqli_connect('localhost', 'root', '', 'zebra-session');
require __DIR__ . '/vendor/autoload.php';
require 'Zebra_Session.php';
$session = new Zebra_Session(
$link,
'sEcUr1tY_c0dE',
0,
true,
false,
60,
'session_data',
false
);
session_start();
$_SESSION['foo'] = 'bar';
⚡ Redis Setup (Optional but Recommended)
Redis is used as a high-speed cache to reduce MySQL load. If Redis is unavailable, sessions automatically fall back to MySQL.
1️⃣ Install Redis Server
Ubuntu / Debian
sudo apt install redis-server
sudo systemctl enable redis
sudo systemctl start redis
Verify Redis:
redis-cli ping
# PONG
2️⃣ Install Redis PHP Client (Predis)
composer require predis/predis
3️⃣ Enable Redis in Zebra_Session
$session->enable_redis([
'host' => '127.0.0.1',
'port' => 6379,
'username' => 'default',
'password' => 'YOUR_PASSWORD',
'ttl' => 3600, // cache lifetime (seconds)
]);
4️⃣ How Redis Integration Works
Action Behavior
Session Read Redis → MySQL fallback Session Write MySQL + Redis Session Destroy MySQL + Redis Redis Down Safe fallback to MySQL
Session keys stored in Redis:
zebra_session:{session_id}
5️⃣ Debug: Check Session Source
echo $session->getLastSource();
// REDIS or MYSQL
🔐 Security
- HttpOnly cookies
- Secure cookies on HTTPS
- Cookie-only sessions
- Optional IP & User-Agent binding
- MySQL session locking
Regenerate Session ID (IMPORTANT)
$session->regenerate_id();
Call this after login or privilege changes.
⚡ Flash Data
$session->set_flashdata('success', 'Profile updated!');
Available for one request only.
🧹 Destroy Session
$session->stop();
Deletes: - Session cookie - MySQL record - Redis cache
📊 Active Sessions
$count = $session->get_active_sessions();
📚 Original Documentation
Full API reference:\ https://stefangabos.github.io/Zebra_Session/Zebra_Session/Zebra_Session.html
📜 License
GNU LGPL-3.0\ Original © Stefan Gabos\ Redis integration added without breaking original behavior.
❤️ Credits
- Zebra_Session -- Stefan Gabos
- Predis -- Redis PHP Client
claytonpulman/zebra_session_plus 适用场景与选型建议
claytonpulman/zebra_session_plus 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 125 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「mysql」 「session」 「flash」 「locking」 「mysqli」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 claytonpulman/zebra_session_plus 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 claytonpulman/zebra_session_plus 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 claytonpulman/zebra_session_plus 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Secure session middleware for Slim 3 framework
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Non-I/O blocking PHP SessionHandler implementation using Nette/Caching with DI Extension for Nette framework
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 125
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: LGPL-3.0-or-later
- 更新时间: 2026-01-14