Zend_Session在_init的bootstrap中

时间:2012-02-21 21:09:41

标签: php zend-framework frameworks

女士们,先生们,

我正在尝试保存会话中上次访问的时间戳。

我选择在我的引导程序中执行此操作。

bootstrap.php中:

<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    /**
     * Bootstrap::_initSaveLastVisitTimestamp()
     * 
     * @return void
     */
    public function _initSaveLastVisitTimestamp()
    {
      Zend_Session::start();
      $session = new Zend_Session_Namespace();
      $session->last_visit = time();
    }
}

的application.ini:

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application_"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0

resources.view.contentType = "text/html; charset=UTF-8"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.view[] =

resources.db.adapter = "PDO_MYSQL"
resources.db.params.dbname = "user_admin"
resources.db.params.username = "user_admin"
resources.db.params.password = "password"

resources.session.saveHandler.class = "Zend_Session_SaveHandler_DbTable"
resources.session.saveHandler.options.name = "session"
resources.session.saveHandler.options.primary.0 = "session_id"
resources.session.saveHandler.options.primary.1 = "save_path"
resources.session.saveHandler.options.primary.2 = "name"
resources.session.saveHandler.options.primaryAssignment.0 = "sessionId"
resources.session.saveHandler.options.primaryAssignment.1 = "sessionSavePath"
resources.session.saveHandler.options.primaryAssignment.2 = "sessionName"
resources.session.saveHandler.options.modifiedColumn = "modified"
resources.session.saveHandler.options.dataColumn = "session_data"
resources.session.saveHandler.options.lifetimeColumn = "lifetime"
[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

不幸的是,这不起作用,但是当我移动这段代码时

<?php
Zend_Session::start();
$session = new Zend_Session_Namespace();
$session->last_visit = time();

到我的IndexController.php然后它工作..

我已经尝试了几乎一切,但我仍然无法让它发挥作用。

任何人都有任何想法?

2 个答案:

答案 0 :(得分:3)

由于您使用的是基于数据库的会话处理程序,因此在使用会话之前需要引导数据库似乎是合理的。

所以,试试这个:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    /**
     * Bootstrap::_initSaveLastVisitTimestamp()
     * 
     * @return void
     */
    public function _initSaveLastVisitTimestamp()
    {
      // Ensure that both the session and the db resources are bootstrapped
      // before executing this
      $this->bootstrap(array('db', 'session'));

      // Actually start the session
      Zend_Session::start();

      // same as before
      $session = new Zend_Session_Namespace();
      $session->last_visit = time();
    }
}

答案 1 :(得分:0)

您需要在bootstrap文件中设置会话:

bootstrap.php中:

<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    /**
     * Bootstrap::_initSession()
     * 
     * @return void
     */
    public function _initSession()
    {
      Zend_Session::start();
      $session = new Zend_Session_Namespace('mySession');
    }
}

然后在您的控制器中获取您的会话变量并设置上次访问的时间戳:

IndexController.php

<?php
$mySession = new Zend_Session_Namespace('mySession');
$mySession->last_visit = time();