作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在我的项目中使用 sfGuard 作为身份验证插件。我想在 session 超时时调用某些客户端和服务器端脚本。我能做到这一点的最佳方法是什么。
求助!非常感谢。
最佳答案
我一直在阅读 sfGuardSecurityUser
它扩展了 sfBasicSecurityUser
类,处理用户身份验证、配置文件、凭据等。
所以,我在sfBasicSecurityUser
中找到了一个函数确定用户 session 是否定时放置称为 isTimedOut
, 还有 setTimedOut
.
如果你想在用户 session 超时时做一些事情,至少在服务器端,你应该监听发生这种情况时抛出的事件。检查此方法:
这可以在 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 and Javascript Workers .这个想法可能是在页面加载时设置一个工作人员,并告诉他数到session_time_out
。 ,然后重定向到登录页面或其他页面。
关于php - 如何在 symfony 中的 sfGuard session 超时时调用自定义函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4553291/
我是一名优秀的程序员,十分优秀!