我在我的项目中使用 sfGuard 作为身份验证插件。我想在会话超时时调用某些客户端和服务器端脚本。我能做到这一点的最好方法是什么。
请帮忙!非常感谢。
我在我的项目中使用 sfGuard 作为身份验证插件。我想在会话超时时调用某些客户端和服务器端脚本。我能做到这一点的最好方法是什么。
请帮忙!非常感谢。
好吧,我一直在阅读sfGuardSecurityUser
它,它扩展了sfBasicSecurityUser
处理用户身份验证、配置文件、凭据等的类。
所以,我找到了一个函数,sfBasicSecurityUser
它可以确定用户会话是否被定时调用isTimedOut
,以及setTimedOut
.
如果您想在用户会话超时时做某事,至少在服务器端,您应该监听发生这种情况时抛出的事件。检查此方法:
这可以在symfony_core_root_dir/lib/user/sfBasicSecurityUser.class.php
public function initialize(sfEventDispatcher $dispatcher, sfStorage $storage, $options = array())
{
// initialize parent
parent::initialize($dispatcher, $storage, $options);
if (!array_key_exists('timeout', $this->options))
{
$this->options['timeout'] = 1800;
}
// force the max lifetime for session garbage collector to be greater than timeout
if (ini_get('session.gc_maxlifetime') < $this->options['timeout'])
{
ini_set('session.gc_maxlifetime', $this->options['timeout']);
}
// read data from storage
$this->authenticated = $storage->read(self::AUTH_NAMESPACE);
$this->credentials = $storage->read(self::CREDENTIAL_NAMESPACE);
$this->lastRequest = $storage->read(self::LAST_REQUEST_NAMESPACE);
if (null === $this->authenticated)
{
$this->authenticated = false;
$this->credentials = array();
}
else
{
// Automatic logout logged in user if no request within timeout parameter seconds
$timeout = $this->options['timeout'];
if (false !== $timeout && null !== $this->lastRequest && time() - $this->lastRequest >= $timeout)
{
if ($this->options['logging'])
{
$this->dispatcher->notify(new sfEvent($this, 'application.log', array('Automatic user logout due to timeout')));
}
$this->setTimedOut();
$this->setAuthenticated(false);
}
}
$this->lastRequest = time();
}
对于客户端,您可能会开始考虑HTML 5 和 Javascript Workers。这个想法可能是在页面加载时设置一个工作人员,并告诉他 count until session_time_out
,然后重定向到登录页面或其他东西。