PHPackages                             voku/session2db - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. voku/session2db

ActiveLibrary[Database &amp; ORM](/categories/database)

voku/session2db
===============

A PHP library acting as a wrapper for PHP's default session handling functions which stores data in a MySQL database, providing both better performance and better security and protection against session fixation and session hijacking.

4.0.4(6y ago)2919.9k7[1 issues](https://github.com/voku/session2db/issues)[2 PRs](https://github.com/voku/session2db/pulls)LGPL-3.0PHPPHP &gt;=7.0.0

Since Jan 26Pushed 1y ago5 watchersCompare

[ Source](https://github.com/voku/session2db)[ Packagist](https://packagist.org/packages/voku/session2db)[ Docs](http://stefangabos.ro/php-libraries/zebra-session/)[ Fund](https://www.paypal.me/moelleken)[ GitHub Sponsors](https://github.com/voku)[ RSS](/packages/voku-session2db/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (38)Used By (0)

[![Build Status](https://camo.githubusercontent.com/4c5880cd1255ea49defdee8b4f180947bb908a6485dede7cf086ff94caea6c35/68747470733a2f2f7472617669732d63692e6f72672f766f6b752f73657373696f6e3264622e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/voku/session2db)[![Coverage Status](https://camo.githubusercontent.com/638eafa18af3ea4edf7879d84e1b99092224ca39cb0257766c6fb09040b59892/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f766f6b752f73657373696f6e3264622f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/voku/session2db?branch=master)[![Codacy Badge](https://camo.githubusercontent.com/b081d42439c96b1069528ec1d83f7d21eaed3c010e4bdb94a8a10c80e008ab35/68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f3833366462373732666639343433623138313033643661366336656533356562)](https://www.codacy.com/app/voku/session2db)[![Latest Stable Version](https://camo.githubusercontent.com/7f533e288c9d7967b722637ea383b529955a612e5061d1226cc01435c841e163/68747470733a2f2f706f7365722e707567782e6f72672f766f6b752f73657373696f6e3264622f762f737461626c65)](https://packagist.org/packages/voku/session2db)[![Total Downloads](https://camo.githubusercontent.com/829341b370aa21aae156989a58f0d5c716378f3a5762ebb7ac3a6165e8ca91f7/68747470733a2f2f706f7365722e707567782e6f72672f766f6b752f73657373696f6e3264622f646f776e6c6f616473)](https://packagist.org/packages/voku/session2db)[![License](https://camo.githubusercontent.com/16e58fbb08d75f4169a06d4e99b064dd2456001d6459320bf04f54333bfc2a9f/68747470733a2f2f706f7365722e707567782e6f72672f766f6b752f73657373696f6e3264622f6c6963656e7365)](https://packagist.org/packages/voku/session2db)[![Donate to this project using Paypal](https://camo.githubusercontent.com/0d6e4d8b50b5983a58205941b1a581b1305903393b7a39da574e3f60af3c7f5b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f70617970616c2d646f6e6174652d79656c6c6f772e737667)](https://www.paypal.me/moelleken)[![Donate to this project using Patreon](https://camo.githubusercontent.com/f9e075baad95563481d35174d43ef50757281abb6bc795d0f473fad452afa030/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f70617472656f6e2d646f6e6174652d79656c6c6f772e737667)](https://www.patreon.com/voku)

👑 Session2DB
============

[](#crown-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.

[](#a-drop-in-replacement-for-phps-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](http://devzone.zend.com/413/trick-out-your-session-handler/) article and [Chris Shiflett](http://shiflett.org/articles/the-truth-about-sessions)'s articles about PHP sessions and based on [Zebra\_Session](https://github.com/stefangabos/Zebra_Session).

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

### Requirements

[](#requirements)

PHP 7.x with the **mysqli extension** activated, MySQL 5.x+ (recommanded: **mysqlnd extension**)

### How to install

[](#how-to-install)

```
composer require voku/session2db
```

### How to use

[](#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!

```
//
// simple (dirty) example
//
