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
Installs: 10
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Forks: 0
pkg:composer/claytonpulman/zebra_session_plus
Requires
- php: >=5.5.2
- predis/predis: ^3.3
Requires (Dev)
- phpstan/phpstan: ^1.12
- squizlabs/php_codesniffer: ^3.10
This package is not auto-updated.
Last update: 2026-01-15 07:15:55 UTC
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