bootstrap _init 中的 Zend_Session

发布于 2025-01-07 14:41:53 字数 2484 浏览 1 评论 0原文

女士们先生们,

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

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

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 时,它就起作用了..

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

有人有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柳絮泡泡 2025-01-14 14:41:54

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

所以,试试这个:

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

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();
    }
}
寻梦旅人 2025-01-14 14:41:54

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

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文