gpt4 book ai didi

php - 如何在 symfony 中的 sfGuard session 超时时调用自定义函数

转载 作者:搜寻专家 更新时间:2023-10-31 21:17:53 24 4
gpt4 key购买 nike

我在我的项目中使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com