La configuración básica para Zend_Session_SaveHandler_DbTable debe tener
al menos cuatro columnas, indicadas en el array de configuración o en el objeto
Zend_Config:
primary, que es la clave primaria y por defecto es simplemente el id
de sesión, que por defecto es una cadena de longitud 32;
modified, que es la marca de tiempo unix de la última fecha de modificación;
lifetime, que es la duración de la sesión
(modified + lifetime > time(););
y data, que son los datos serializados almacenados en la sesión
Ejemplo 65.18. Configuración básica
CREATE TABLE `session` ( `id` char(32), `modified` int, `lifetime` int, `data` text, PRIMARY KEY (`id`) );
//get your database connection ready
$db = Zend_Db::factory('Pdo_Mysql', array(
'host' =>'example.com',
'username' => 'dbuser',
'password' => '******',
'dbname' => 'dbname'
));
//you can either set the Zend_Db_Table default adapter
//or you can pass the db connection straight to the save handler $config
Zend_Db_Table_Abstract::setDefaultAdapter($db);
$config = array(
'name' => 'session',
'primary' => 'id',
'modifiedColumn' => 'modified',
'dataColumn' => 'data',
'lifetimeColumn' => 'lifetime'
);
//create your Zend_Session_SaveHandler_DbTable and
//set the save handler for Zend_Session
Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($config));
//start your session!
Zend_Session::start();
//now you can use Zend_Session like any other time
También puede usar múltiples columnas en su clave primaria con
Zend_Session_SaveHandler_DbTable.
Ejemplo 65.19. Uso de una clave primaria multi-columna
CREATE TABLE `session` (
`session_id` char(32) NOT NULL,
`save_path` varchar(32) NOT NULL,
`name` varchar(32) NOT NULL DEFAULT '',
`modified` int,
`lifetime` int,
`session_data` text,
PRIMARY KEY (`Session_ID`, `save_path`, `name`)
);
//setup your DB connection like before
//NOTE: this config is also passed to Zend_Db_Table so anything specific
//to the table can be put in the config as well
$config = array(
'name' => 'session', //table name as per Zend_Db_Table
'primary' => array(
'session_id', //the sessionID given by PHP
'save_path', //session.save_path
'name', //session name
),
'primaryAssignment' => array(
//you must tell the save handler which columns you
//are using as the primary key. ORDER IS IMPORTANT
'sessionId', //first column of the primary key is of the sessionID
'sessionSavePath', //second column of the primary key is the save path
'sessionName', //third column of the primary key is the session name
),
'modifiedColumn' => 'modified', //time the session should expire
'dataColumn' => 'session_data', //serialized data
'lifetimeColumn' => 'lifetime', //end of life for a specific record
);
//Tell Zend_Session to use your Save Handler
Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($config));
//start your session
Zend_Session::start();
//use Zend_Session as normal