Skip navigation.

Introduction to Using Sessions with ADOdb

ADOdb is a database abstraction library (Download). Read detailed documentation on ADOdb's session support.

Introduction

PHP4 is packed with good features. One of the most popular is session variables. These are variables that persist throughout a session, as the user moves from page to page. Session variables are great holders of state information and other useful stuff.

To use session variables, call session_start() at the beginning of your web page, before your HTTP headers are sent. Then for every variable you want to keep alive for the duration of the session, store it in $_SESSION[$variable_name]. By default, the session handler will keep track of the session by using a cookie. You can save objects or arrays in session variables also (but be warned that this is buggy in some versions of PHP4).

The default method of storing sessions is to store it in a file. However if you have multiple web servers, or need to do special processing of each session, or require notification when a session expires, you need to override the default session storage behaviour. One method is to use a database backed session handler. This stores the session information as records in a database table that can be shared across multiple servers.

There is a great article by Ying Zhang called Custom Session Handlers in PHP4 that explains how this is done.

The above article only supports MySQL and DBM. I decided to generalise Ying Zhang's ideas to use ADOdb, a database wrapper library that supports MySQL, PostgreSQL, Oracle, Microsoft SQL Server, Sybase, Informix, Access, FoxPro, Interbase, ODBC and ADO for true cross-platform database independence.

14 July 2002 Update: Since ADOdb 2.10, we perform a crc32 check on the session data, and only update when the session data has changed. This provides a massive improvement in performance when session variables rarely change.

Installation

For ADODB's implementation, you need to create a table called sessions in your database. It should have the following structure (mysql):
 create table sessions (
       SESSKEY char(32) not null,
       EXPIRY int(11) unsigned not null,
       EXPIREREF varchar(64),
       DATA text not null,
       primary key (SESSKEY), 
       key (EXPIRY) 
)

In your PHP.ini, make sure session.save_handler=user. Hint thanks to goghs.

Usage

Here is example of how it is used. Click on the refresh button to see the AVAR variable increase.
<?php
# define connection parameters
$ADODB_SESSION_DRIVER='database driver, eg. mysql or ibase'; 
$ADODB_SESSION_CONNECT='server to connect to';
$ADODB_SESSION_USER ='user';
$ADODB_SESSION_PWD ='password';
$ADODB_SESSION_DB ='database';

include('/path/to/adodb/adodb.inc.php');
include('/path/to/adodb/session/adodb-session.php');

# initialize session variable to avoid 
# warnings of uninitialized variables
$_SESSION['AVAR'] = 0;

# load the session variables from database
session_start();

# test code
$_SESSION['AVAR'] += 1;
print "<p> variable={$_SESSION['AVAR']} </p>";

?>

Advanced Features

We support notification on session expiry (useful for unlocking resources when a user logs out unexpectedly), encrypted sessions, and storing sessions in LOBs. Read more in the detailed documentation. Thanks to Bryan Orthner some documentation problems fixed.