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

标签 php session symfony1 session-timeout sfguard

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

相关文章:

php - 混淆 API 签名公钥和私钥对

node.js - 在expressjs服务器和vuejs客户端之间使用快速 session

jquery - Symfony ajax 字段验证

symfony1 - symfony - 嵌套集和父/子节点的渲染

php - 使用 .htaccess 将 .ics 文件作为 .php 文件运行

php - 搜索某些 STRING 的 XML 属性

java - 实体对象持久化时出现空指针异常

php - "Cannot send session cache limiter - headers already sent"

php - 选择 Zend 还是 Symfony?为什么?

php - 创建 Linux Screen session 并获取其 PID