Home arrow PHP arrow Page 4 - Using Advanced Functions to Maintain the State of Applications with PHP Sessions

Getting the MySQL-based session module complete: listing MySQL processing classes - PHP

Here we are again. This is part two of the series “Maintaining the state of applications with PHP sessions." In three parts, this series ranges from the basics of session management in PHP, such as creating, registering session data, and destroying sessions, to exploring advanced concepts, like working with different session storage modules and creating custom session handling objects.

  1. Using Advanced Functions to Maintain the State of Applications with PHP Sessions
  2. Tweaking the PHP session storage module: using the “session_set_save_handler ()” function
  3. Going deeper into PHP session management: creating a MySQL-based session storage module
  4. Getting the MySQL-based session module complete: listing MySQL processing classes
By: Alejandro Gervasio
Rating: starstarstarstarstar / 12
May 03, 2006

print this article



As I promised, here’s the source code for the couple of MySQL processing classes used within the definition of the prior callback functions:

class MySQL {
    var $conId; // connection identifier
    var $host; // MySQL host
    var $user; // MySQL username
    var $password; // MySQL password
    var $database; // MySQL database
    // constructor
    function MySQL($options=array()){
        // validate incoming parameters
            foreach($options as $parameter=>$value){
                    trigger_error('Invalid parameter
            // connect to MySQL
        else {
            trigger_error('No connection parameters were
    // connect to MYSQL server and select database
    function connectDB(){
            trigger_error('Error connecting to the
            trigger_error('Error selecting
    // perform query
    function query($query){
            trigger_error('Error performing query
        // return new Result object
        return new Result($this,$this->result); 

class Result {
    var $mysql; // instance of MySQL object
    var $result; // result set
    function Result(&$mysql,$result){
    // fetch row
    function fetchRow(){
        return mysql_fetch_array($this->result,MYSQL_ASSOC);
    // count rows
    function countRows(){
            return false;
        return $rows;
    // count affected rows
    function countAffectedRows(){
            trigger_error('Error counting affected
        return $rows;
    // get ID from last inserted row
    function getInsertID(){
            trigger_error('Error getting ID',E_USER_ERROR);
        return $id;
    // seek row
    function seekRow($row=0){
            trigger_error('Error seeking data',E_USER_ERROR);
    function getQueryResource(){
        return $this->result;

Right. Now that I showed you the pair of MySQL processing classes used by the previous callback functions, let me set up an example which implements this user-defined session storage system, by the respective “session_set_save_handler()” function. Have a look at the code listed below:

// include classes
require_once 'mysqlclass.php';
require_once 'resultclass.php';
// connect to MySQL
$db=&new MySQL(array
// use 'session_set_save_handler function'
// register some session variables

That’s it. After connecting to MySQL, the above script uses the “session_set_save_handler()” in order to register all the callback functions that you saw before, and as a result, all the session data will be stored in a sample “sessions” database table. By tweaking the correct session settings within the php.ini file, in conjunction with implementing this MySQL-driven session storage module, it’s possible to construct a more efficient and secure session management mechanism than the one provided as default by PHP. As you’ve seen, the experience can be instructive and educational, so why don’t you try it for yourself?

Wrapping up

That’s all for the moment. Over this second part of the series, I explored the powerful “session_save_path()” and “session_set_save_handler()” functions. Particularly, this last function can be extremely helpful for developing a custom session management system that uses a MySQL database table for storing session-related data, instead of conventional flat files.

Since this approach is used in many situations where a personalized session storage mechanism is preferred over the default offered by PHP, in the last article, I’ll encapsulate all the pertinent callback functions defined before within a class. In this way, the entire session handling process can be centralized at only one handler object. Therefore, don’t miss the next part!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: