PHP Classes

How to Use a PHP Session Handler That Supports MySQL, Redis, Memcached, and Files Using the Package Session Handlers Collection: Store PHP sessions in different types of storage

Recommend this page to a friend!
     
  Info   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-11-09 (Yesterday) RSS 2.0 feedNot enough user ratingsTotal: 52 This week: 5All time: 10,664 This week: 17Up
Version License PHP version Categories
session-handlers 2.0.7MIT/X Consortium ...7Databases, Files and Folders, User Ma..., L..., P...
Description 

Author

This package can store PHP sessions in different types of storage.

It provides several session handler classes that store and retrieve PHP session data in different storage system types with an option to make session variables read-only, to avoid the variable can be changed.

The package provides a function to let developers set the preferred session handler.

Currently, it can store session data in:

- Files

- Redis server

- Memcached server

- MySQL database table

- Cookie on client side

The package also supports accessing session values without changing the values in read-only mode.

Innovation Award
PHP Programming Innovation award nominee
October 2024
Nominee
Vote
PHP developers often use sessions to store values about the access of each user who is accessing a site.

Session values are usually stored in a persistent container like files or database tables.

PHP stores session values in files by default. However, it also supports registering new session handler classes that can store session values in other types of containers.

This package provides session handlers that can store session values in different types of containers.

It also supports a read-only mode that PHP developers can use in requests that do not require changing the session variables.

When the read-only mode is enabled, session access can be faster because the session handler will not perform the session data write step.

Manuel Lemos
Picture of Ramesh Narayan Jangid
  Performance   Level  
Name: Ramesh Narayan Jangid <contact>
Classes: 10 packages by
Country: India India
Age: 49
All time rank: 3061196 in India India
Week rank: 9 Up2 in India India Up
Innovation award
Innovation award
Nominee: 5x

Winner: 3x

Details

Session Handlers

Collection of Mostly used Session Handlers

  • Supports File / MySql / Redis / Memcached / Cookie based Session Handlers
  • Supports Readonly mode as well for all the above mentioned Session Handlers

Example

<?php
include __DIR__ . '/CustomSessionHandler/Session.php';

// Turn on output buffering
ob_start();        

// Initialise Session Handler
Session::initSessionHandler('File');
// Session::initSessionHandler('MySql');
// Session::initSessionHandler('Redis');
// Session::initSessionHandler('Memcached');
// Session::initSessionHandler('Cookie');

// Start session in readonly mode
// Use when user is already logged in and we need to authorise the client cookie.
Session::start_readonly();

// Auth Check
if (!isset($_SESSION) || !isset($_SESSION['id'])) {
    die('Unauthorised');
}

// Start session in normal (read/write) mode.
// Use once client is authorised and want to make changes in $_SESSION
Session::start_rw_mode();
$_SESSION['id'] = 1;

Database Table for MySql

CREATE TABLE IF NOT EXISTS `sessions` (
    `sessionId` CHAR(64) NOT NULL,
    `lastAccessed` INT UNSIGNED NOT NULL,
    `sessionData` MEDIUMBLOB,
    PRIMARY KEY (`sessionId`)
) ENGINE=InnoDB;

  Files folder image Files (13)  
File Role Description
Files folder imageCustomSessionHandler (2 files, 1 directory)
Accessible without login Plain text file Example.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file Login.php Aux. Configuration script
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (13)  /  CustomSessionHandler  
File Role Description
Files folder imageContainers (7 files)
  Plain text file CustomSessionHandler.php Class Class source
  Plain text file Session.php Class Class source

  Files folder image Files (13)  /  CustomSessionHandler  /  Containers  
File Role Description
  Plain text file CookieBasedSessionContainer.php Class Class source
  Plain text file FileBasedSessionContainer.php Class Class source
  Plain text file MemcachedBasedSessionContainer.php Class Class source
  Plain text file MySqlBasedSessionContainer.php Class Class source
  Plain text file RedisBasedSessionContainer.php Class Class source
  Plain text file SessionContainerHelper.php Class Class source
  Plain text file SessionContainerInterface.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:52
This week:5
All time:10,664
This week:17Up