延长CakePHP的生命周期Cookie [英] Extending the life of the CakePHP Session Cookie

查看:90
本文介绍了延长CakePHP的生命周期Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  ('Session',array(
'defaults'=>'database',
'cookie'=>'mycookie',
'timeout'=> 4320 // 3 days
));

这是大部分按预期工作。会话存储在数据库中,cookie被正确命名,并且cookie在3天后被删除。



我得到了来自 http://book.cakephp.org/2.0/en/development/ sessions.html#built-in-session-handlers-configuration



不幸的是,这不是我想要的。我希望cookie在3天后被删除,但我想它是最后一次用户在网站上活动后的3天。换句话说:



1)用户在星期一访问网站,Cookie设置为到期星期三。



2)星期一的用户访问网站,直到星期四才再次回来,因此,必须生成新的cookie。



一开始我认为这可能是添加 Session.autoRegenerate 但这似乎没有任何帮助。即使使用此设置,即使用户在整个3天的时间内仍在网站上活动,Cookie仍然似乎遭受了3天后被删除的命运。

/ Controller / AppController中处理它。 php 文件。



这样也许这样。



/UsersController.php

  public login(){
if($ this-> request- >是('post')){
if($ this-> Auth-> login()){
$ this-> Session-> write('Auth.timeout' ,strtotime('+ 3 days'));
return $ this-> redirect($ this-> Auth-> redirect());
} else {
$ this-> Session-> setFlash(__('Username或password is incorrect'),'default',array(),'auth');
}
}
}

AppController.php

  public function beforeFilter(){
if($ this-& > user()&& $ this-> Session-> read('Auth.timeout')< = strtotime('now')){
$ this-> Auth->登出();
$ this-> Session-> setFlash(__(您的会话已过期。'),'default',array(),'auth');
}
}


In my Cake 2 app I have the following code in APP/Config/core.php:

Configure::write('Session', array(
    'defaults' => 'database',
            'cookie' => 'mycookie',
            'timeout' => 4320 //3 days
));

This is working mostly as expected. The sessions are stored in the database, the cookie is named correctly and the cookie is deleted after 3 days.

I got the above example from http://book.cakephp.org/2.0/en/development/sessions.html#built-in-session-handlers-configuration

Unfortunately, this isn't exactly what I want. I want the cookie to be deleted after 3 days, but I want it to be 3 days after the last time the user was active on the site. In other words:

1) User visits site on Monday, cookie is set to expire Wednesday. However, he comes back on Tuesday so now the cookie will expire on Thursday.

2) User visits site on Monday and doesn't come back again until Thursday, so a new cookie has to be generated.

At first I thought that it might be a matter of adding Session.autoRegenerate but that doesn't seem to be of any help. Even with this set, the cookie still seems to suffer the same fate of being deleted after 3 days even if the user was active on the site for the entire 3-day period.

解决方案

Since PHP auto renews the session cookie with each request, you should consider storing a separate timeout variable in the session and handle it in your /Controller/AppController.php file.

Something like this maybe.

/Controller/UsersController.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');
        }
    }
}

/Controller/AppController.php

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');
  }
}

这篇关于延长CakePHP的生命周期Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆