PHP Classes

File: README.md

Recommend this page to a friend!
  Classes of Ramesh Narayan Jangid   Session Handlers Collection   README.md   Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: Session Handlers Collection
Store PHP sessions in different types of storage
Author: By
Last change: Optimised handler class
Date: 9 days ago
Size: 1,226 bytes
 

Contents

Class file image Download

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;