简体   繁体   中英

Zend_Session in _init of bootstrap

Ladies and gentlemen,

I am trying to save the timestamp of the last visit in a session.

I chose to do this in my bootstrap.

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

Unfortunately this is not working, but when I move this piece of code

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

to my IndexController.php then it works..

I've tried almost everything but I still cano't get it to work.

Anyone has any idea?

Since you are using a db-based session handler, it seems reasonable that you would need the db to be bootstrapped before you can use the sessions.

So, try this:

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

You need to set your session in bootstrap file :

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');
    }
}

then get your session variable in your controller and set last visit's timestamp:

IndexController.php

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM