Global Session Management

Zend_Session_SaveHandler_DbTable

The basic setup for Zend_Session_SaveHandler_DbTable must at least have four columns, denoted in the config array or Zend_Config object: primary, which is the primary key and defaults to just 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

Example #1 Basic Setup

  1. span style="color: #ff0000;">`session` (
  2.   `id` char(32),
  3.   `modified``lifetime``data``id`)
  4. );
  1. //get your database connection ready
  2. 'Pdo_Mysql''host'        =>'example.com',
  3.     'username'    => 'dbuser',
  4.     'password'    => '******',
  5.     'dbname'    => 'dbname'
  6. ));
  7.  
  8. //you can either set the Zend_Db_Table default adapter
  9. //or you can pass the db connection straight to the save handler $config
  10. 'name'           => 'session',
  11.     'primary'        => 'id',
  12.     'modifiedColumn' => 'modified',
  13.     'dataColumn'     => 'data',
  14.     'lifetimeColumn' => 'lifetime'
  15. );
  16.  
  17. //create your Zend_Session_SaveHandler_DbTable and
  18. //set the save handler for Zend_Session
  19. //start your session!
  20. //now you can use Zend_Session like any other time

You can also use Multiple Columns in your primary key for Zend_Session_SaveHandler_DbTable.

Example #2 Using a Multi-Column Primary Key

  1. span style="color: #ff0000;">`session` (
  2.     `session_id``save_path``name`'',
  3.     `modified``lifetime``session_data``Session_ID`, `save_path`, `name`)
  4. );
  1. //setup your DB connection like before
  2. //NOTE: this config is also passed to Zend_Db_Table so anything specific
  3. //to the table can be put in the config as well
  4. 'name'              => 'session', //table name as per Zend_Db_Table
  5.     'primary''session_id',   //the sessionID given by PHP
  6.         'save_path',    //session.save_path
  7.         'name',         //session name
  8.     ),
  9.     'primaryAssignment'//you must tell the save handler which columns you
  10.         //are using as the primary key. ORDER IS IMPORTANT
  11.         'sessionId', //first column of the primary key is of the sessionID
  12.         'sessionSavePath', //second column of the primary key is the save path
  13.         'sessionName', //third column of the primary key is the session name
  14.     ),
  15.     'modifiedColumn'    => 'modified',     //time the session should expire
  16.     'dataColumn'        => 'session_data', //serialized data
  17.     'lifetimeColumn'    => 'lifetime',     //end of life for a specific record
  18. );
  19.  
  20. //Tell Zend_Session to use your Save Handler
  21. //start your session
  22. //use Zend_Session as normal

Global Session Management