PHP Classes

File: Example.php

Recommend this page to a friend!
  Classes of Ramesh Narayan Jangid (Sharma)   Session Handlers Collection   Example.php   Download  
File: Example.php
Role: Example script
Content typex: text/plain
Description: Example script
Class: Session Handlers Collection
Store PHP sessions in different types of storage
Author: By
Last change: Refactoring
Date: 1 month ago
Size: 1,338 bytes
 

Contents

Class file image Download
<?php

// For Mongo DB (composer require mongodb/mongodb)
// require __DIR__ . '/vendor/autoload.php';

require_once __DIR__ . '/AutoloadSessionHandler.php'; // phpcs:ignore

use CustomSessionHandler\Session;

// Turn on output buffering
ob_start();

// Session Runtime Configuration
$options = [];

// Initialize Session Handler
Session::initSessionHandler(sessionMode: 'File', options: $options);
// Session::initSessionHandler(sessionMode: 'MySql', options: $options);
// Session::initSessionHandler(sessionMode: 'PostgreSql', options: $options);
// Session::initSessionHandler(sessionMode: 'MongoDb', options: $options);
// Session::initSessionHandler(sessionMode: 'Redis', options: $options);
// Session::initSessionHandler(sessionMode: 'Memcached', options: $options);
// Session::initSessionHandler(sessionMode: 'Cookie', options: $options);

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

if (isset(
$_SESSION)) {
   
print_r(value: $_SESSION);
}

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

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