开发者

Storing Sessions in DB Table Not Working (using Zend_Session_SaveHandler_DbTable)

开发者 https://www.devze.com 2022-12-10 10:42 出处:网络
This is my table: CREATE TABLE `Sessions` ( `id` varchar(32) NOT NULL, `modified` int(11) default NULL, `lifetime` int(11) default NULL,

This is my table:

CREATE TABLE `Sessions` (
`id` varchar(32) NOT NULL,
`modified` int(11) default NULL,
`lifetime` int(11) default NULL,
`data` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB

This is in my bootstrap:

$sessionConfig = array( 
'name'           => 'Sessions',      //table name as per Zend_Db_Table 
'primary'        => 'id',   //the sessionID given by php 
'modifiedColumn' => 'modified',     //time the session should expire 
'dataColumn'     => 'data', //serialized data 
'lifetimeColumn' => 'lifetime'      //end of life for a specific record 
); 
$saveHandler = new Zend_Session_SaveHandler_DbTable($sessionConfig); 
//cookie persist for 30 days 
Zend_Session::rememberMe($seconds = (60 * 60 * 24 * 30)); 

//make the session persist for 30 days 
$saveHandler->setLifetime($seconds) 
    ->setOverrideLifetime(true); 
//similarly, 
//$saveHandler->setLifetime($seconds, true); 
Zend_Session::setSaveHandler($saveHandler); 
Zend_Session::start(); 

开发者_StackOverflow社区When I log in, nothing ever gets written to the Sessions table and I am logged out on the very next pageview.

Any ideas? I'm trying to have my users be perpetually logged in. Am I missing something in my login controller possibly?


I just managed to get this working:

My application.ini:

resources.db.isDefaultTableAdapter = true
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.dbname = "dbname"
resources.db.params.username = "username"
resources.db.params.password = "password"

my bootstrap.php:

protected function _initSession() {
    $resource = $this->getPluginResource('db');
    $dbAdapter = $db = $resource->getDbAdapter();
    Zend_Registry::set("db", $dbAdapter);
    Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter);
    $config = array(
        'name' => 'session',
        'primary' => 'id',
        'modifiedColumn' => 'modified',
        'dataColumn' => 'data',
        'lifetimeColumn' => 'lifetime',
        'db' => $dbAdapter
    );

    Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($config));
    Zend_Session::start();
}

This function was placed as first function in the bootstrap.php, because sessions are started, when you construct a Zend_Session_Namespace object for the first time. If you do this, before the _initSession()-function got called, a standard file-based session may be started.

Finally, the session.sql:

DROP TABLE IF EXISTS `session`;


CREATE TABLE `session` (

  `id` char(32) NOT NULL DEFAULT '',

  `modified` int(11) DEFAULT NULL,

  `lifetime` int(11) DEFAULT NULL,

  `data` text,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Somewhere i read that the table must be InnoDB.


You have to initialize your DB handler before telling Zend_Session to use the DB, either by setting a default adapter for Zend_Db, or passing your adapter in the config array as 'db'.


Maybe you have to put Zend_Session::start(); before anything else on the page... ?


Had the same problem with an implementation of redis as session handler.

For me, putting the method _initSession as first method in my bootstrap class works.

Hope it will help someone.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号