Thursday, July 2, 2009

Zend Framework: Storing session data in database

In one of my previous post I wrote how to save session data in the database. The reason behind saving session data in database is that data become more secure and can be easily retrieved later on.
Let’s look how simple and easy it is to save session data in database using Zend_Session_SaveHandler_DbTable in Zend Framework.
1. Creating database table for holding session data
Before writing code you will need to create a table in the database for holding session data. Execute the following query for this purpose.
CREATE TABLE `session` (
`id` char(32),
`modified` int,
`lifetime` int,
`data` text,
PRIMARY KEY (`id`)
);


2.Making necessary configuration in your bootstrap file.
You will only need only few lines of code in your bootstrap file.
After making database configuration in your bootstrap file, write the following code.
$config = array(
'name' => 'session',
'primary' => 'id',
'modifiedColumn' => 'modified',
'dataColumn' => 'data',
'lifetimeColumn' => 'lifetime'
);

Zend_Session::setSaveHandler(new
Zend_Session_SaveHandler_DbTable($config));
Zend_Session::start();

In the $config array we tell the name of the table that will store the session information, primary key of the table, column that will store the session modification timestamp(*nix timestamp), column that will store the session data, and the column that save the information about how long the session will last.

After defining this configuration array we call setSaveHandler() method- static method, of the Zend_Session. This setSaveHandler() method accept Zend_Session_SaveHandler_DbTable object. Zend_Session_SaveHandler_DbTable take configuration array as an argument.

We have now done all the necessary configuration. The only thing we will need to do now is to start our session. Session can be started in the bootstrap file, however it’s not necessary for the session to be started here. You can start it in any controller you needed.

Anywhere in your controller, write
$namespace = new Zend_Session_Namespace();
$namespace->name = "faheem";


and check your session table. The table will not only hold session id, but also the modified timestamp of when session was modified, life time of the session and the session data in special format.
Try it. Its really cool.

9 comments:

  1. And how to cleare session data from db?
    Who must do it ?

    ReplyDelete
  2. If you know how to delete data from table using Zend_Db, you can easily delete data from session data.

    ReplyDelete
  3. How could i relate the saved datas to a user if the browser has been closed?

    If the user comes back :
    1 - can i check if this session was his ?
    2 - if i've linked user->id to session->id, is it possible to name the new session after the old one or should i get datas from the old saved session and store them in the new one ?

    Might be a stupid question -> beginner

    Dede

    ReplyDelete
  4. when i have run the any applicatin in zend framework the SERVER eeror is occour loke CGI script so plz give me response as soon as possible
    plz

    ReplyDelete
  5. $namespace->name = "faheem";

    instead of faheem ,what should i specify??

    ReplyDelete
  6. is it really necessary to use "ID" in the database? i have seen many tutorials and still wondering the purpose of the id. why exactly do they use it?

    Smith | salon uniforms

    ReplyDelete