Global Session Management | Zend_Session Programmer's Reference Guide | Zend_Soap

# 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. CREATE TABLE `session` (
2. `id` char(32),
3. `modified` int,
4. `lifetime` int,
5. `data` text,
6. PRIMARY KEY (`id`)
7. );

1. //get your database connection ready
2. $db = Zend_Db::factory('Pdo_Mysql', array (
3. 'host' =>'example.com',
4. 'username' => 'dbuser',
5. 'password' => '******',
6. 'dbname' => 'dbname'
7. ));
9. //you can either set the Zend_Db_Table default adapter
10. //or you can pass the db connection straight to the save handler $config
11. Zend_Db_Table_Abstract::setDefaultAdapter($db);
12. $config = array (
13. 'name' => 'session',
14. 'primary' => 'id',
15. 'modifiedColumn' => 'modified',
16. 'dataColumn' => 'data',
17. 'lifetimeColumn' => 'lifetime'
18. );
20. //create your Zend_Session_SaveHandler_DbTable and
21. //set the save handler for Zend_Session
22. Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($config));
24. //start your session!
25. Zend_Session::start();
27. //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. CREATE TABLE `session` (
2. `session_id` char(32) NOT NULL,
3. `save_path` varchar(32) NOT NULL,
4. `name` varchar(32) NOT NULL DEFAULT '',
5. `modified` int,
6. `lifetime` int,
7. `session_data` text,
8. PRIMARY KEY (`Session_ID`, `save_path`, `name`)
9. );

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. $config = array (
5. 'name' => 'session', //table name as per Zend_Db_Table
6. 'primary' => array (
7. 'session_id', //the sessionID given by PHP
8. 'save_path', //session.save_path
9. 'name', //session name
10. ),
11. 'primaryAssignment' => array (
12. //you must tell the save handler which columns you
13. //are using as the primary key. ORDER IS IMPORTANT
14. 'sessionId', //first column of the primary key is of the sessionID
15. 'sessionSavePath', //second column of the primary key is the save path
16. 'sessionName', //third column of the primary key is the session name
17. ),
18. 'modifiedColumn' => 'modified', //time the session should expire
19. 'dataColumn' => 'session_data', //serialized data
20. 'lifetimeColumn' => 'lifetime', //end of life for a specific record
21. );
23. //Tell Zend_Session to use your Save Handler
24. Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($config));
26. //start your session
27. Zend_Session::start();
29. //use Zend_Session as normal

Global Session Management | Zend_Session Programmer's Reference Guide | Zend_Soap