From YiPs Wiki (i powered)

PHP: DB 2 Session

(click to open)

Quick Page Table of Contents

Scanning…

PHP DB2 session support

[goto main page]

[goto document links]

This page demonstrates replacing PHP sessions with DB2 session support.

DB2 session

Download:

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

sessifs.php:
<?php
session_start();
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*
/tmp/sess_qskbigect526dvt228g4067ru6
:
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:

DB2SessionHandler.php:
<?php
class DB2SessionHandler
{
    private $saveError;
    private $conn;
    private $debug;

    public function __construct() {
      // take over session support
      session_set_save_handler(
        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).

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

Author(s)

Tony “Ranger” Cairns - IBM i PHP / PASE

Retrieved from http://youngiprofessionals.com/wiki/index.php/PHP/DB2Session
Page last modified on February 14, 2013, at 01:04 PM EST