Zend Framework: Storing Session in Database

Storing Session in Database is one of the ways to handle session data in the environment with multiple web servers. Even so the documentation about Zend_Session_SaveHandler_DbTable and Zend_Application_Resource_Session is available you still have to know a little bit more in order to use them. I ‘m going to reveal this little thing. Please let me know in comments if this post was helpful.

Summary

Assumptions

Your Zend Framework version is 1.10
It’s necessary for your application to store session in database

Create Database Table

The table which stores session data must at least have four columns: the session id which by default is a string of length 32; modified, which is the unix timestamp of the last modified date; lifetime, which is the lifetime of the session (modified + lifetime > time();); and data, which is the serialized data stored in the session

      CREATE TABLE `session` (
        `id` char(32),
        `modified` int,
        `lifetime` int,
        `data` text,
        PRIMARY KEY (`id`)
      );

Initialize a session SaveHandler in application.ini

Important: You need to define database resource before session resource.

# Database
 resources.db.adapter = "Pdo_Mysql"
 resources.db.params.host = "localhost"
 resources.db.params.username = "webuser"
 resources.db.params.password = "XXXX"
 resources.db.params.dbname = "test"
 resources.db.isDefaultTableAdapter = true

# Session
 resources.session.use_only_cookies = true
 resources.session.gc_maxlifetime = 864000
 resources.session.remember_me_seconds = 864000

 resources.session.saveHandler.class = "Zend_Session_SaveHandler_DbTable"

 resources.session.saveHandler.options.name = "session"
 resources.session.saveHandler.options.primary = "id"
 resources.session.saveHandler.options.modifiedColumn = "modified"
 resources.session.saveHandler.options.dataColumn = "data"
 resources.session.saveHandler.options.lifetimeColumn = "lifetime"

Start session in bootstrap class

In order to start session you need to add the following method to your Bootstrap class

    /**
     * Start session
     */
    public function _initCoreSession()
    {
        $this->bootstrap('db');
        $this->bootstrap('session');
        Zend_Session::start();
    }

Saving and Retrieving data from session

You can use the following code to set and retrieve value form session.

class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
        // default namespace
        $namespace = new Zend_Session_Namespace();
        $namespace->foo = 100;
        // mySpace namespace
        $namespace = new Zend_Session_Namespace('mySpace');
        $namespace->foo = 100;
        // action body
    }
}

Source code

https://github.com/denisura/zfSessionDatabase

Related posts:

  1. Custom Error Pages in Zend Framework
  2. Zend Framework: Getting result set from Stored Procedure

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

If the information on this site helps you and you'd like to support this blog follow ad banner below.
It's the easiest thing you can do, isn't it? ;)

6 Comments »

 
 

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*