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

1.2.0 2026-01-14 22:02 UTC

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
  • mysqli or PDO extension

๐Ÿ“‚ 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