延长CakePHP的生命周期Cookie [英] Extending the life of the CakePHP Session 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天后被删除的命运。
这样也许这样。
/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屋!
相关文章
- 延长变量的生命周期;
- initializer_list 返回的生命周期延长;
- 延长线程变量的生命周期;
- 延长字符串切片的借用生命周期;
- 延长线程的生命周期(C ++ 11);
- EWS:同步状态cookie的生命周期;
- auto&&延长临时对象的生命周期?;
- 生命周期;
- Rust 生命周期错误预期具体生命周期,但发现绑定生命周期;
- 应用生命周期;
- 临时生命周期;
- preferenceActivity生命周期;
- 实体生命周期;
- 如何延长由ref.resetPassword()生成的密码的生命周期?;
- const 引用类成员是否会延长临时对象的生命周期?;
- FacesContext的生命周期?;
- 预期的绑定生命周期参数,找到了具体的生命周期;
- 在Android的WebView / CookieSyncManager会话cookie的生命周期;
- PHP-session_set_cookie_params(),生命周期无效;
- servlet 生命周期和过滤器生命周期之间的区别;
- 为什么本地WLClient生命周期绑定到活动的生命周期;
- angularjs 1.x 支持生命周期和生命周期结束;
- rust 生命周期参数必须比静态生命周期长;
- 访问令牌生命周期;
- WinForms 事件生命周期;
PHP最新文章
- 请求头字段Access-Control-Allow-Headers在预检响应中不允许Access-Control-Allow-Headers;
- 路由问题导致Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException错误;
- 什么是NCFB和NOFB模式?;
- 警告:mysqli_connect():(HY000 / 1045):访问被拒绝用户'用户名'@'localhost'(使用密码:是);
- 如何处理致命错误:cURL错误7:无法连接到xxxx端口443;
- 参数3传递给GuzzleHttp\Client :: request()必须是数组类型,给定字符串;
- phpMyAdmin的#2054无法登录到MySQL服务器;
- SSL错误SSL3_GET_SERVER_CERTIFICATE:证书验证失败;
- 在PHPExcel中设置字体颜色,字体和字体大小;
- 如何解决cURL错误(7):无法连接到主机?;