1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.session.savehandler.dbtable">
4 <title>Zend_Session_SaveHandler_DbTable</title>
7 The basic setup for <classname>Zend_Session_SaveHandler_DbTable</classname> must at least
8 have four columns, denoted in the config array or <classname>Zend_Config</classname> object:
9 primary, which is the primary key and defaults to just the session
10 id which by default is a string of length 32;
11 modified, which is the unix timestamp of the last modified date;
12 lifetime, which is the lifetime of the session
13 (<command>modified + lifetime > time();</command>);
14 and data, which is the serialized data stored in the session
17 <example id="zend.session.savehandler.dbtable.basic">
18 <title>Basic Setup</title>
20 <programlisting language="SQL"><![CDATA[
21 CREATE TABLE `session` (
30 <programlisting language="php"><![CDATA[
31 //get your database connection ready
32 $db = Zend_Db::factory('Pdo_Mysql', array(
33 'host' =>'example.com',
34 'username' => 'dbuser',
35 'password' => '******',
39 //you can either set the Zend_Db_Table default adapter
40 //or you can pass the db connection straight to the save handler $config
41 Zend_Db_Table_Abstract::setDefaultAdapter($db);
45 'modifiedColumn' => 'modified',
46 'dataColumn' => 'data',
47 'lifetimeColumn' => 'lifetime'
50 //create your Zend_Session_SaveHandler_DbTable and
51 //set the save handler for Zend_Session
52 Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($config));
55 Zend_Session::start();
57 //now you can use Zend_Session like any other time
62 You can also use Multiple Columns in your primary key for
63 <classname>Zend_Session_SaveHandler_DbTable</classname>.
66 <example id="zend.session.savehandler.dbtable.multi-column-key">
67 <title>Using a Multi-Column Primary Key</title>
69 <programlisting language="SQL"><![CDATA[
70 CREATE TABLE `session` (
71 `session_id` char(32) NOT NULL,
72 `save_path` varchar(32) NOT NULL,
73 `name` varchar(32) NOT NULL DEFAULT '',
77 PRIMARY KEY (`Session_ID`, `save_path`, `name`)
81 <programlisting language="php"><![CDATA[
82 //setup your DB connection like before
83 //NOTE: this config is also passed to Zend_Db_Table so anything specific
84 //to the table can be put in the config as well
86 'name' => 'session', //table name as per Zend_Db_Table
88 'session_id', //the sessionID given by PHP
89 'save_path', //session.save_path
90 'name', //session name
92 'primaryAssignment' => array(
93 //you must tell the save handler which columns you
94 //are using as the primary key. ORDER IS IMPORTANT
95 'sessionId', //first column of the primary key is of the sessionID
96 'sessionSavePath', //second column of the primary key is the save path
97 'sessionName', //third column of the primary key is the session name
99 'modifiedColumn' => 'modified', //time the session should expire
100 'dataColumn' => 'session_data', //serialized data
101 'lifetimeColumn' => 'lifetime', //end of life for a specific record
104 //Tell Zend_Session to use your Save Handler
105 Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($config));
108 Zend_Session::start();
110 //use Zend_Session as normal