📜 ⬆️ ⬇️

Zend Framework. Save the session in the database.

Small article on Zend Framework.
More precisely, the interpretation of the section from the Zend_Framework user manual.

Original: Zend_Session_SaveHandler_DbTable

This very Zend_Session_SaveHandler_DbTable allows you to configure Zend_Session so that information about sessions (and session data) is stored in the database.

Zend_Session_SaveHandler_DbTable


To store a session, we need to save at least four of its parameters. Namely:
  1. sid - This is the session ID, and is 32 long
  2. modified - Date of session modification, in unix timestamp format
  3. lifetime - the time of life of this session itself (modified + lifetime> time ())
  4. data - The actual data stored in the session is serialized.

With the data you need to store, we decided to create a table in the database.
')
CREATE TABLE `session` (
`id` char(32),
`modified` int,
`lifetime` int,
`data` text,
PRIMARY KEY (`id`)
);

By default, the session ID will be the primary key.
Further. (Everything else is written in index.php)

// , $db
$db = Zend_Db::factory('Pdo_Mysql', array(
'host' =>'example.com',
'username' => 'dbuser',
'password' => '******',
'dbname' => 'dbname'
));

//
Zend_Db_Table_Abstract::setDefaultAdapter($db);

// config Zend_Session_SaveHandler_DbTable
$config = array(
'name' => 'session',
'primary' => 'id',
'modifiedColumn' => 'modified',
'dataColumn' => 'data',
'lifetimeColumn' => 'lifetime'
);

// Zend_Session_SaveHandler_DbTable
// Zend_Session
Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($config));

// ( )
Zend_Session::start();

$ config
name - the name of the table in the database
primary - table field which is the primary key in the table
modifiedColumn - Table field Modification dates
dataColumn - table field stub data
lifetimeColumn - The table field is a harsh life time

The second example in the tutorial considers a variant using several values ​​as the primary key. I did not rewrite it here.

For primary, specify an array containing a list of fields that are included in the primary key.

Through primaryAssignment we assign the appropriate values.
sessionId - Session ID
sessionSavePath - Aabsure session path
sessionName - the name of the session

Source: https://habr.com/ru/post/42714/


All Articles