【问题标题】:How to set the session timeout in Zend Framework 2如何在 Zend Framework 2 中设置会话超时
【发布时间】:2014-08-18 15:59:16
【问题描述】:

我没有太多 Zend 经验,想在 Zend 中更改某人的登录代码以使会话不过期。

看来代码是基本行为:

    $adapter = $this->getAuthService()->getAdapter();
    $adapter->setIdentity($email)->setCredential($password);

    $result = $this->getAuthService()->authenticate();

我必须做些什么才能使会话不过期或至少将会话设置为特定时间?

现在用户不会长时间保持登录状态,我认为它可能只是依赖于默认的 php 设置行为,例如 gc_maxlifetime 的标准 24 分钟。

Zend_Session 和 AuthService 有什么联系?

【问题讨论】:

  • 问题的标题是How to set the session timeout in Zend Framework 2 但它是关于ZF1 !!

标签: php zend-framework session-timeout


【解决方案1】:

我认为您需要的是延长会话 cookie 的生命周期。为此,您可以使用 rememberMe() 函数。在验证用户身份后执行此操作:

Zend_Session::rememberMe(1209600); //1209600/3600 = 336 hours => 336/24 = 14 days

但是延长会话 cookie 的生命周期不会有任何效果,因为在 gc_maxlifetime 时间过去后服务器将无法识别该 cookie。

要解决这个问题,您必须确保 PHP 的 session.gc_maxlifetime 设置至少与您传递给 rememberMe() 的值相同。

我不知道这是否是好的做法,但你可以增加PHP Session Garbage Collection Time

在您的Bootstrap.php 文件中,您可以这样做:

protected function __initSession() {
ini_set('session.gc_maxlifetime', 1209600);  // maxlifetime = 14 days
ini_set('session.gc_divisor', 1);
Zend_Session::start();
}

或在.htaccess 文件中:(14 天)

php_value session.gc_maxlifetime 1209600

【讨论】:

    【解决方案2】:

    这里是如何设置会话超时:

    $session = new Zend_Session_Namespace( 'Zend_Auth' );
    $session->setExpirationSeconds( 60 ); // 60 seconds
    

    【讨论】:

    • 您的代码可用于比默认值更快过期,但我认为它不会延长会话超出@987654322 @这是原始海报所要求的。
    【解决方案3】:

    这里是如何设置会话超时:

    $session = new Zend_Session_Namespace( 'Zend_Auth' );
    $session->setExpirationSeconds( 60 ); // 60 seconds
    

    这对我很有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-26
      • 2013-01-30
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 2014-11-20
      • 2020-01-03
      相关资源
      最近更新 更多