From YiPs Wiki (i powered)

PHP: DB 2 Session

(click to open)

Quick Page Table of Contents


PHP DB2 session support

[goto main page]

[goto document links]

This page demonstrates replacing PHP sessions with DB2 session support.

DB2 session


Traditionally, PHP programs use file system (IFS) to save/restore PHP variable $_SESSION information like this example sessifs.php.

if (isset($_SESSION[$key1])) {
  $data1 = $_SESSION[$key1];

Unfortunately, using IFS can lead to a very messy /tmp directory and in some cases, slow/inconsistent performance as number of session files grows with heavy usage by 1000′s of different users. You began to see the effects by simply looking in the /tmp directory for sess* files:

> ls /tmp/sess*
100's - 1000's

An alternative is to use DB2 records to hold PHP session data by override PHP session API handlers. PHP provides session_set_save_handler() API to allow us to redirect all session API actions directly in/out of DB2 records (cool).

Bill of materials (view on-line):

In the example we created a PHP class DB2SessionHandler that takes over PHP session support. Our new DB2 based session support needs to implement only a few PHP session APIs to take over:

class DB2SessionHandler
    private $saveError;
    private $conn;
    private $debug;

    public function __construct() {
      // take over session support
        array($this, "open"),
        array($this, "close"),
        array($this, "read"),
        array($this, "write"),
        array($this, "destroy"),
        array($this, "gc")
see view link (or download zip)
// take over php session handler now
new DB2SessionHandler();

And here is example program that uses the new DB2 session support (looks the same as IFS session support yes).

require_once("DB2SessionHandler.php"); // DB2 took over session support
if (isset($_SESSION[$key1])) {
  $data1 = $_SESSION[$key1];


Tony “Ranger” Cairns - IBM i PHP / PASE

Retrieved from
Page last modified on February 14, 2013, at 01:04 PM EST