PHP Classes
elePHPant
Icontem

PHP Session MySQL Handler: Handler for storing session data in MySQL

Recommend this page to a friend!
  Info   View files Documentation   View files View files (14)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-03-17 (1 month ago) RSS 2.0 feedNot enough user ratingsTotal: 75 This week: 7All time: 8,753 This week: 110Up
Version License PHP version Categories
session2db 1.0.14GNU General Publi...5.3PHP 5, Databases, User Management
Description Author

This class implements a handler for storing session data in MySQL.

This is class based on Zebra_Session from Stefan Gabos that registers session handler functions to store and retrieve session data from MySQL databases using MySQLi.

It uses row locks to lock only individual session database table records while the current script session is being accessed.

  Performance   Level  
Name: Lars Moelleken <contact>
Classes: 16 packages by
Country: Germany Germany
Age: 29
All time rank: 1760111 in Germany Germany
Week rank: 4 Up1 in Germany Germany Up
Innovation award
Innovation award
Nominee: 7x

Details

Join the chat at https://gitter.im/voku/session2db Build Status Coverage Status Scrutinizer Code Quality Codacy Badge SensioLabsInsight Dependency Status Latest Stable Version Total Downloads Latest Unstable Version PHP 7 ready License

Session2DB

A drop-in replacement for PHP's default session handler which stores session data in a database, providing both better performance and better security and protection against session fixation and session hijacking.

Session2DB implements session locking - a way to ensure that data is correctly handled in a scenario with multiple concurrent AJAX requests.

It is also a solution for applications that are scaled across multiple web servers (using a load balancer or a round-robin DNS) and where the user's session data needs to be available. Storing sessions in a database makes them available to all of the servers!

The library supports "flashdata" - session variable which will only be available for the next server request, and which will be automatically deleted afterwards. Typically used for informational or status messages (for example: "data has been successfully updated").

Session2DB is was inspired by John Herren's code from the Trick out your session handler article and Chris Shiflett's articles about PHP sessions.

The code is heavily commented and generates no warnings/errors/notices when PHP's error reporting level is set to E_ALL.

Features

  • acts as a wrapper for PHP?s default session handling functions, but instead of storing session data in flat files it stores them in a database, providing better security and better performance
  • it is a drop-in and seamingless replacement for PHP?s default session handler: PHP sessions will be used in the same way as prior to using the library; you don?t need to change any existing code!
  • implements row locks, ensuring that data is correctly handled in scenarios with multiple concurrent AJAX requests
  • because session data is stored in a database, the library represents a solution for applications that are scaled across multiple web servers (using a load balancer or a round-robin DNS)
  • has comprehensive documentation
  • the code is heavily commented and generates no warnings/errors/notices when PHP?s error reporting level is set to E_ALL

Requirements

PHP 5+ with the mysqli extension activated, MySQL 4.1.22+

How to install

composer require voku/session2db

How to use

After installing, you will need to initialise the database table from the install directory from this repo, it will containing a file named session_data.sql. This file contains the SQL code that will create a table that is used by the class to store session data. Import or execute the SQL code using your preferred MySQL manager (like phpMyAdmin or the fantastic Adminer) into a database of your choice.

*Note that this class assumes that there is an active connection to a MySQL database and it does not attempt to create one!

<?php
    use voku\db\DB;
    use voku\helper\Session2DB;

    // include autoloader
    require_once 'composer/autoload.php';

    $db = DB::getInstance('yourDbHost', 'yourDbUser', 'yourDbPassword', 'yourDbName');

    // example
    // $db = DB::getInstance('localhost', 'root', '', 'test');
    
    $session = new Session2DB();

    // from now on, use sessions as you would normally
    // this is why it is called a "drop-in replacement" :)
    $_SESSION['foo'] = 'bar';

    // data is in the database!
  Files folder image Files  
File Role Description
Files folder imageinstall (3 files)
Files folder imagesrc (1 directory)
Files folder imagetests (2 files)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .styleci.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file license.txt Doc. Documentation
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  install  
File Role Description
  Accessible without login HTML file index.html Doc. Documentation
  Accessible without login Plain text file install.txt Doc. Documentation
  Accessible without login Plain text file session_data.sql Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imagevoku (1 directory)

  Files folder image Files  /  src  /  voku  
File Role Description
Files folder imagehelper (1 file)

  Files folder image Files  /  src  /  voku  /  helper  
File Role Description
  Plain text file Session2DB.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script
  Plain text file SimpleSessionTest.php Class Class source

Downloadsession2db-2017-03-17.zip 16KB
Downloadsession2db-2017-03-17.tar.gz 13KB
Install with ComposerInstall with Composer
Needed packages  
Class DownloadWhy it is needed Dependency
Simple MySQLi Class Download .zip .tar.gz for the DB connect Required
 Version Control Unique User Downloads Download Rankings  
 100%
Total:75
This week:7
All time:8,753
This week:110Up