[英]How to set the session timeout in Zend Framework 2
I don't have much Zend experience and want to change someone's login code in Zend to make the session not expire. 我没有太多的Zend经验,并且想要在Zend中更改某人的登录代码以使会话不会过期。
It seems the code is basic behavior: 看来代码是基本行为:
$adapter = $this->getAuthService()->getAdapter();
$adapter->setIdentity($email)->setCredential($password);
$result = $this->getAuthService()->authenticate();
What do I have to do to make the session not expire or to at least set the session for a specific time? 我该怎么做才能使会话不过期或至少将会话设置为特定时间?
Right now the user doesn't stay logged in for long, I think perhaps it is just relying on default php settings behavior like the standard 24 minutes for the gc_maxlifetime. 现在,用户不会长时间保持登录状态,我认为这可能只是依赖默认的php设置行为,例如gc_maxlifetime的标准24分钟行为。
What is the connection between Zend_Session and AuthService? Zend_Session和AuthService之间有什么联系?
I think what you need is extending the session cookie lifetime. 我认为您需要延长会话cookie的寿命。 For that you could use the
rememberMe()
function. 为此,您可以使用
rememberMe()
函数。 Do this after authenticating the user : 在验证用户身份后执行此操作:
Zend_Session::rememberMe(1209600); //1209600/3600 = 336 hours => 336/24 = 14 days
But extending the session cookie lifetime will not have an effect because the server will not recognize that cookie after gc_maxlifetime
time elapsed. 但是,延长会话cookie的生存期不会产生任何效果,因为在
gc_maxlifetime
时间过后,服务器将无法识别该cookie。
To deal with this, you must ensure the PHP session.gc_maxlifetime
setting is at least the same value as the value you passed to rememberMe()
. 要解决此问题,必须确保PHP
session.gc_maxlifetime
设置至少与传递给rememberMe()
的值相同。
I don't know if it's good practice or not but you can increase the PHP Session Garbage Collection Time
. 我不知道这是否是一个好习惯,但是您可以增加
PHP Session Garbage Collection Time
。
In your Bootstrap.php
file, you can do this : 在您的
Bootstrap.php
文件中,您可以执行以下操作:
protected function __initSession() {
ini_set('session.gc_maxlifetime', 1209600); // maxlifetime = 14 days
ini_set('session.gc_divisor', 1);
Zend_Session::start();
}
Or in the .htaccess
file : (for 14 days) 或在
.htaccess
文件中:(14天)
php_value session.gc_maxlifetime 1209600
Hope this helps. 希望这可以帮助。
here is how to set the session timeout : 这是设置会话超时的方法:
$session = new Zend_Session_Namespace( 'Zend_Auth' );
$session->setExpirationSeconds( 60 ); // 60 seconds
here is how to set the session timeout : 这是设置会话超时的方法:
$session = new Zend_Session_Namespace( 'Zend_Auth' );
$session->setExpirationSeconds( 60 ); // 60 seconds
This works great for me. 这对我来说很棒。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.