PHP Classes
elePHPant
Icontem

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

Recommend this page to a friend!
  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 (6 years ago) RSS 2.0 feedNot yet rated by the usersTotal: 262 All time: 7,297 This week: 853Up
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
Innovation award
Innovation award
Nominee: 2x

Details
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
MySQL.

The class was originally written by Rich Smith 2007-05-02 and
published on the web at
http://www.devshed.com/c/a/PHP/Storing-PHP-Sessions-in-a-Database/

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

CONFIGURING CLASS

   public  $sesstable = 'new_sessions';
   
      Change to the DB table name you are using for session
      management
      
   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.
      
CREATING THE DATABASE

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)
  ) ENGINE = MYISAM;
  
USING THE CLASS

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

<?php
require_once('db_connect.inc.php'); // file where you set mdb2 handle
require_once('sessions_apc.php');
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:
   http://www.devshed.com/c/a/PHP/Storing-PHP-Sessions-in-a-Database/
Pear::MDB2:
   http://pear.php.net/MDB2
APC:
   http://pecl.php.net/package/APC
  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:262
This week:0
All time:7,297
This week:853Up