【问题标题】:How to run an UPDATE query on SESSION TIMEOUT如何在 SESSION TIMEOUT 上运行 UPDATE 查询
【发布时间】:2016-09-29 08:29:21
【问题描述】:

我正在开发一个使用 CakePHP 2.8 构建的项目。在登录时,我将 FLAG 设置为 1,在注销时将其设置为 0,以便该帐户一次可以在单台计算机上登录。在这部分之前它工作得很好。

我面临的问题是会话超时。我很困惑当会话超时时如何在数据库中将标志设置为 0。有什么方法可以在会话超时时运行更新查询。

我正在使用 CORE 配置文件来设置 SESSION 超时限制,如下所示:

Configure::write('Session', array(
    'defaults' => 'php',
    'timeout' => 30, // The session will timeout after 30 minutes of inactivity
    'cookieTimeout' => 1440, // The session cookie will live for at most 24 hours, this does not effect session timeouts
    'checkAgent' => false,
    'autoRegenerate' => true, // causes the session expiration time to reset on each page load
));

这是我的注销功能

public function logout() {
    $id = $this->Auth->User('id');
    $this->User->updateAll(array('WebLoggedIn'=>0), array('User.id'=>$id));
    $this->Auth->logout();
    // redirect to the home
    return $this->redirect('/');
}

【问题讨论】:

标签: php session cakephp model-view-controller session-timeout


【解决方案1】:

那行不通

问题中的想法行不通。与会话相关的移动部分,问题中的配置是:

  • 存储在服务器上的文件,其中包含序列化的会话数据,每次写入会话时都会更新
  • 标准的 php cron 作业,用于删除已过期的会话文件(请参阅 /etc/cron.d/php5 或等效项)
  • 浏览器 cookie,将用户的浏览器会话链接到服务器上的文件

当用户的会话超时 - 这意味着他们提供的会话 ID 与服务器上的文件不对应,或者他们根本没有提供会话 cookie。过期时没有“嘿,此会话已过期”事件,并且不能保证用户会提供旧的会话 ID 供您检查它是否有效。

工作提案

一个简单的(这也意味着幼稚并且可能很容易绕过)解决方案是不存储布尔值,而是将会话到期的时间存储在数据库中。 IE。有类似这样的代码:

// App Controller
public function beforeFilter()
{
    $userId = $this->Auth->user('id');
    if ($userId) {
        $this->User->updateAll(
            array('active_session_expires'=> time() + (30 * 60)), 
            array('User.id'=>$id)
        );
    }
}

在用户控制器中:

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            if ($this->Auth->user('active_session_expires') > time()) {
                $this->Flash->error('You are still logged in somewhere else');
                return $this->logout();
            }

            $this->User->updateAll(
                array('active_session_expires'=> time() + (30 * 60)), 
                array('User.id'=> $this->Auth->user('id'))
            );
            return $this->redirect($this->Auth->redirectUrl());
        }
        $this->Flash->error(__('Invalid username or password, try again'));
    }
}

public function logout()
{
    $id = $this->Auth->User('id');
    if ($id) {
        $this->User->updateAll(
            array('active_session_expires'=> time()),
            array('User.id'=>$id)
        );
        $this->Auth->logout();
    }
    return $this->redirect('/');
}

即每次他们做某事时——更新数据库以跟踪他们的活动。如果他们在现有会话到期之前尝试登录 - 立即将其注销。预计需要测试/修改此示例代码,它提供给您一个想法,不一定是完整且有效的解决方案。

这类似于an answer you've already been linked to

【讨论】:

  • 感谢您的建议,但即使用户处于活动状态,上述功能也会在 30 分钟后将用户踢出。我想在 30 分钟不活动时结束会话。任何建议这样做@AD7six
  • 我认为您误读了答案,这不是真的the above function will kick out the user after 30 min even if the user is active。没有代码可以注销用户,除了如果他们尝试登录 - 并且只有尝试登录会话被注销。
  • 对不起,我看错了。但是,如果我使用上面的代码,我如何检查用户是否在 30 分钟内处于非活动状态,那么它需要注销并在那时重置标志。 @AD7six
  • then it need to be logged out - ?你不知道,这就是 Session.timeout 配置设置的用途。如果您确实想按照您的要求做,请阅读active_session_expires 值并根据需要使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-02
相关资源
最近更新 更多