嗨:我使用的是最新版本的Zend Framework(1.9.3PL1)。我在.ini
中设置了以下内容; Bootstrap session resources
resources.session.save_path = APPLICATION_PATH "/../data/sessions"
resources.session.use_only_cookies = true
resources.session.remember_me_seconds = 864000
接下来我想在我的引导程序中初始化我的会话:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initSession()
{
// What goes here!?
}
}
我的问题是,initSession函数有什么用处?它应该返回什么,如果有的话?
此外,如果我刚刚在那里开始会话,则它无法识别.ini配置(例如,save_path未更改)。但是,如果将开头移动到控制器,则会识别.ini配置。
编辑:可能的解决方案是:
protected function _initSession()
{
// Based on http://framework.zend.com/issues/browse/ZF-6651
$session = $this->getPluginResource('session');
$session->init();
Zend_Session::start();
}
答案 0 :(得分:11)
如果在应用程序配置中使用resources.session.*
- 选项,则必须在引导程序中使用_initSession()
方法,因为这些方法将覆盖插件资源session
的执行({ {1}})。配置文件中Zend_Application_Resource_Session
- 选项的唯一退出将确保会话将根据您的选项进行初始化。
请阅读Zend_Application, Theory of Operation,了解有关所谓的资源方法和资源插件的详细讨论。
答案 1 :(得分:7)
Stefan非常正确,您将覆盖使用这些应用程序选项的默认会话资源。
如果要定义自己的_initSession()方法并仍然访问这些选项,请使用以下内容:
protected function _initSession()
{
$options = $this->getOptions();
$sessionOptions = array(
'save_path' => $options['resources']['session']['save_path']
);
Zend_Session::setOptions($options);
Zend_Session::start();
}
答案 2 :(得分:3)
protected function _initSession()
{
$config = array();
$config['db'] = array('adapter'=>'PDO_SQLITE',
'params' => array('dbname'=> ROOT.'/data/tmp.db3')
);
$config['SaveHandler'] = array(
'name' => 'sessions', //table name as per Zend_Db_Table
'primary' => array(
'id', //the sessionID given by PHP
'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' => 'data', //serialized data
'lifetimeColumn' => 'lifetime', //end of life for a specific record
);
$config['lifetime'] = 60*60*24*30;
$config['options'] = array (
'bug_compat_42' => '',
'bug_compat_warn' => '',
'cache_expire' => '180',
'cache_limiter' => 'nocache',
'cookie_domain' => '',
'cookie_httponly' => '',
'cookie_lifetime' => $config['lifetime'],
'cookie_path' => '/',
'cookie_secure' => '0',
'entropy_file' => '',
'entropy_length' => '0',
'gc_divisor' => '1000',
'gc_maxlifetime' => '1440',
'gc_probability' => '1',
'hash_bits_per_character' => '5',
'hash_function' => '0',
'name' => 'TaMeR_SESSID',
'referer_check' => '',
'save_handler' => 'user',
'save_path' => '',
'serialize_handler' => 'php',
'use_cookies' => '1',
'use_only_cookies' => 'on',
'use_trans_sid' => '0',
'strict' => false,
'remember_me_seconds' => $config['lifetime'],
'throw_startup_exceptions' => true,
);
$db = Zend_Db::factory($config['db']['adapter'], $config['db']['params']);
if( ! in_array('sessions', $db->listTables())) {
$sql = "CREATE TABLE sessions (";
$sql .= "id TEXT, ";
$sql .= "path TEXT, ";
$sql .= "name TEXT DEFAULT '', ";
$sql .= "modified INTEGER, ";
$sql .= "lifetime INTEGER, ";
$sql .= "data TEXT, ";
$sql .= "PRIMARY KEY (id, path, name)";
$sql .= ");";
$db->exec($sql);
}
Zend_Db_Table_Abstract::setDefaultAdapter($db);
Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($config['SaveHandler']));
Zend_Session::setOptions($config['options']);
Zend_Session::start();
}