延长CakePHP会话Cookie的生命周期

延长CakePHP会话Cookie的生命周期,cakephp,Cakephp,在我的Cake 2应用程序中,app/Config/core.php中有以下代码: Configure::write('Session', array( 'defaults' => 'database', 'cookie' => 'mycookie', 'timeout' => 4320 //3 days )); 这与预期的基本一致。会话存储在数据库中,cookie命名正确,cookie在3天后被删除 上面的例子我是从

在我的Cake 2应用程序中,app/Config/core.php中有以下代码:

Configure::write('Session', array(
    'defaults' => 'database',
            'cookie' => 'mycookie',
            'timeout' => 4320 //3 days
));
这与预期的基本一致。会话存储在数据库中,cookie命名正确,cookie在3天后被删除

上面的例子我是从

不幸的是,这不是我想要的。我希望cookie在3天后被删除,但我希望它在用户上次在站点上活动的3天后被删除。换言之:

1) 用户周一访问该网站,cookie将于周三到期。然而,他会在周二回来,所以现在饼干会在周四过期

2) 用户在周一访问该网站,直到周四才会回来,所以必须生成一个新的cookie


起初我认为这可能是一个添加
Session.autoRegenerate
的问题,但这似乎没有任何帮助。即使使用此设置,cookie仍然会在3天后被删除,即使用户在整个3天的时间内都在站点上活动。

因为PHP会在每次请求时自动更新会话cookie,您应该考虑在会话中存储一个单独的超时变量,并在<代码> /控制器/AppHealth.php < /Cult>文件中处理它。< /P> 也许是这样的

/Controller/userscocontroller.php

public login() {
  if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            $this->Session->write('Auth.timeout', strtotime('+3 days'));
            return $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
        }
    }
}
public function beforeFilter() {
  if ($this->Auth->user() && $this->Session->read('Auth.timeout') <= strtotime('now')) {
    $this->Auth->logout();
    $this->Session->setFlash(__(Your session expired.'), 'default', array(), 'auth');
  }
}
/Controller/AppController.php

public login() {
  if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            $this->Session->write('Auth.timeout', strtotime('+3 days'));
            return $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
        }
    }
}
public function beforeFilter() {
  if ($this->Auth->user() && $this->Session->read('Auth.timeout') <= strtotime('now')) {
    $this->Auth->logout();
    $this->Session->setFlash(__(Your session expired.'), 'default', array(), 'auth');
  }
}
公共函数beforeFilter(){
如果($this->Auth->user()&&&$this->Session->read('Auth.timeout')Auth->logout();
$this->Session->setFlash(uuuu(您的会话已过期)'),'default',array(),'auth');
}
}

这看起来是一个很好的方法,可以确保用户在3天未登录后返回时注销,但当cookie在3天的活动后过期时,它不会让用户保持登录状态。我是否认为唯一的方法是将cookie设置为在未来某个非常遥远的时间过期,然后使用您的方法来确保他们在不活动的3天后必须重新验证?我想我误解了这个问题(我以为你想让他们在不活动的情况下在3天后超时)。如果您将会话超时设置为3天,PHP应在用户每次活动时更新过期时间。因此,在没有我的建议的情况下,它应按您希望的方式工作。如果它不更新,则这是另一个问题。好的,无论如何感谢您的帮助。我已否决了该答案,以便其他查看该答案的人意识到我的sp没有答案具体的问题已经发布了。不过,我认为你的答案应该保留,因为它确实包含了对其他寻求解决相关问题的人有用的信息。