PHP Classes

MDB2 Session Manager: Handler for storing session data with MDB and APC

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in Bookmark in
  Info   View files View files (2)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings  
2009-12-29 (5 years ago) RSS 2.0 feedNot yet rated by the usersTotal: 251 All time: 7,172 This week: 1,156Up
Version License PHP version Categories  
apc-mdb2-sessions 1.0Freeware4Databases, User Management, Cache
Description Author  

This class is a replacement handler for storing session data with MDB2 and APC.

It can be used to store PHP sessions in any database supported by PEAR MDB2.

The class also stores and retrieves session data from APC cache to access session data faster.

Picture of Michael A. Peters
Name: Michael A. Peters <contact>
Classes: 6 packages by
Country: United States United States
Age: 42
All time rank: 877111 in United States United States
Week rank: 462 Up43 in United States United States Up
Innovation award
Innovation award
Nominee: 2x

This class uses a database for session management opposed to
flat file. It replaces the built in php session management.

In addition to using a database for storing session data, if
your server is equipped with APC it will cache the session data
for a brief time reducing the number of database calls needed
for your session based web application to properly function.

APC is a soft dependency, the class will use it if it is available
but the class is 100% fully functional on servers without APC.

Note that even though APC will reduce necessary database calls when
reading session data, writing session data still always will require
a database call. This is necessary because data in an APC cache can
be deleted before it expires if the cache runs out of allocated
memory or is otherwise completely dumped.

Database connection is done with Pear::MDB2.
MDB2 is a hard dependency. The class should work with any database
that is properly supported by MDB2, but I have only tested it with

The class was originally written by Rich Smith 2007-05-02 and
published on the web at

I ported the class to use MDB2 and added the APC cache functions.


   public  $sesstable = 'new_sessions';
      Change to the DB table name you are using for session
   private $apcSalt = '2d8lyds45a@&0KLybafz';
      Used to obfuscate the sessions in the APC cache to make
      it more difficult for a cracker to get session IDs if they
      manage to get a dump of the APC cache. It would be a good
      idea to change the string to something else on your install
      of this class.
   private $apcMaxLife = 1500;
      Lifetime of the session cache. It is intentionally somewhat
      short (25 minutes) to reduce the likelihood of session data
      filling your APC cache causing other cached content to be
      dropped. Most user sessions will be less than 25 minutes on
      most web sites, and when it does expire in cache, the session
      data can still be retrieved from the database, so no harm is
      done to session management by using a short cache life.

The following works to create a database in MySQL. Adjust it for
other databases.

CREATE TABLE new_sessions (
  session_id varchar(32) NOT NULL default '',
  session_data text,
  expires int(11) NOT NULL default '0',
  PRIMARY KEY  (session_id)

Before your php script starts a session, require the file containing
the class and pass it the handle of your MDB2 connection. For example:

require_once(''); // file where you set mdb2 handle
ini_set('session.cookie_httponly',1); // suggested for security
$sess = new SessionManager($mdb2); // initialize the class
session_start(); // start the session management

Original Class by Rich Smith:
  Files folder image Files  
File Role Description
Accessible without login Plain text file README.txt Doc. README file
Plain text file sessions_apc.php Class Class File

 Version Control Unique User Downloads Download Rankings  
 0%Total:251All time:7,172
 This week:0This week:1,156Up