Tags

, , , , , , , , , , , , ,

When working on projects which uses 2 or more servers, for example in a project which runs on load balanced servers, keeping session for all the servers is a great issue. There are different ways to deal with this issue, like;

.memcached

. storing session in db.

Here I am explaining on how to save session details in db tables. I am explaining this step by step

First proceed with creating a new table for saving session details.

CREATE TABLE sessions( id varchar(40) NOT NULL, access int(10), data text, PRIMARY KEY (id));

Now this table handles session details, session id, session access details – for expire details, data for session data.

Now we need to manually give the session handler details. To set a session handler we need to have the following code; session_set_save_handler(‘_open’,’_close’,’_read’,’_write’,’_destroy’,’_clean’);

All the arguments mentioned in the function need to be implemented and can be added any where, but this function, session_set_save_handler need to be the given before session_start() function.

The following are the functionality for each of the functions;
_open : This is used to open a new session
_ close : Close an opened session
_read : read a particular session.
_write : write into an opened session.
_destroy : remove session
_clean : clean up session values

Note : All these functions are automatically called when a corresponding event is being triggered. For example, when a session data is called for, _read() function is automatically called for, with the required parameters.

Note : PHP has its own session handling mechanism. We are just adding a bit of tweak to it.

I am including below sample code for completing the functionality;

<?php
 session_set_save_handler(‘_open’,’_close’,’_read’,’_write’,’_destroy’,’_clean’);
 // DB connection details
 define(“_HOST_NAME”, <host name>);
 define(“_USER_NAME”, <username>);
 define(“_PASSWORD”, <password>);

 function _open(){
    global $_sess_db;
    if (mysql_connect(_HOST_NAME,_USER_NAME,_PASSWORD)) {
            return mysql_select_db(‘techzilla’, $_sess_db);
    }
    return false;
}

 function _close(){
    global $_sess_db;
    return mysql_close($_sess_db);
}

function _read($id){
    global $_sess_db;
    $id = mysql_real_escape_string($id);

    $sql = “SELECT data FROM sessions WHERE id = ‘$id'”;
    if($result = mysql_query($sql)){
        if(mysql_num_rows($result)){
            $record = mysql_fetch_assoc($result);
            return $record[“data”];
        }
    }
    return “”;
}

function _write($id,$data){
    global $_sess_db;
    $access = time();
    $id = mysql_real_escape_string($id);
    $data = mysql_real_escape_string($data);
    $access = mysql_real_escape_string($access);

    $sql = “REPLACE INTO sessions VALUES(‘$id’,$access,’$data’)”;
    return mysql_query($sql,$_sess_db);
}

function _destroy($id){
    global $_sess_db;
    $id = mysql_real_escape_string($id);
    $sql = “DELETE FROM sessions WHERE id = ‘$id'”;
    return mysql_query($sql, $_sess_db);
}

function _clean($max){
    global $_sess_db;
    $old = time() – $max;
    $old = mysql_real_escape_string($old);

    $sql = “DELETE FROM sessions WHERE access < ‘$old'”;
    return mysql_query($sql, $_sess_db);
}
?>

PHP uses session.serialize_handler for serializing the session data.

Advertisements