php - 在数据库表中存储 session 不起作用(使用 Zend_Session_SaveHandler_DbTable)

标签 php mysql database zend-framework session

这是我的 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

这是在我的 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(); 

当我登录时,没有任何内容写入 session 表,我在下一次页面浏览时注销。

有什么想法吗?我正在尝试让我的用户永久登录。我的登录 Controller 中是否可能缺少某些内容?

最佳答案

我刚刚设法让它工作:

我的应用程序.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"

我的 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();
}

这个函数作为第一个函数放在 bootstrap.php 中,因为当你第一次构造一个 Zend_Session_Namespace 对象时 session 就开始了。如果这样做,在调用 _initSession() 函数之前,可能会启动一个标准的基于文件的 session 。

最后,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;

我在某处读到该表必须是 InnoDB。

关于php - 在数据库表中存储 session 不起作用(使用 Zend_Session_SaveHandler_DbTable),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1638145/

相关文章:

php - 基于SQL Dump建立SQL Server

mysql - SQL 请求,其变量等于 WHERE 子句中的当前行列值

数据库优化 : What's faster searching by integers OR short strings?

sql - 为所选记录生成插入脚本?

javascript - 更新数据库后返回更新的对象而不是计数

php - 意外转换为 bool 值?

php - 为什么 cookie 在 CodeIgniter 中不起作用?

php - 阻止浏览器询问用户是否要刷新页面

mysql - 将 mysql 数据库导出到 csv 文件的工具

MYSQLWhere语句/子句被忽略