问题描述
||
我正在尝试使用\'Remember Me \'使我的login()函数正常工作。
function login() {
if ($this->Auth->user()) {
if (!empty($this->data) && $this->data[\'User\'][\'remember_me\']) {
$cookie = array();
$cookie[\'username\'] = $this->data[\'User\'][\'username\'];
$cookie[\'password\'] = $this->data[\'User\'][\'password\'];
$this->Cookie->write(\'Auth.User\',$cookie,true,COOKIE_EXPIRE);
unset($this->data[\'User\'][\'remember_me\']);
}
$this->LogDetail->Write(\'activity\',\'has logged IN\');
$this->redirect($this->Auth->redirect());
}
if (empty($this->data)) {
$cookie = $this->Cookie->read(\'Auth.User\');
if (!is_null($cookie)) {
if ($this->Auth->login($cookie)) {
$this->Session->destroy(\'Message.Auth\'); # clear auth message,just in case we use it.
$this->LogDetail->Write(\'activity\',\'has been authenticated via cookie and is Now logged IN\');
$this->redirect($this->Auth->redirect());
} else {
$this->LogDetail->Write(\'activity\',\'attempted to gain access with an invalid cookie\');
$this->Cookie->destroy(\'Auth.User\'); # delete invalid cookie
$this->Session->setFlash(\'Invalid cookie\');
$this->redirect(\'login\');
}
}
}
}
它首先检查用户是否在会话中得到了授权。
如果在会话中授权了用户,它将把他们重定向到预期的页面。
如果用户由于已提交登录表单而在会话中获得了授权,它将检查是否选择了“记住我”,然后在重定向之前创建一个cookie。
如果未在会话中授权用户,该函数将检查Auth.User cookie的存在,然后尝试Auth-> login($ cookie)。
这是发生问题的地方。
如果没有会话但拥有Auth.User cookie的用户访问该站点,它将永久重定向,并反复写入写入日志“已通过cookie进行身份验证,现在登录”,直到浏览器终止。
我很困惑,因为$ this-> Auth-> login($ cookie)返回true,但是SESSION没有被更新!
Auth-> login($ cookie)如何返回true,但是会话的auth信息仍未设置(导致无限循环)?
在无限循环中查看Firebug中Cookie的情况时,我注意到CAKEPHP会话Cookie在此过程中不断更改值
我还应该提到,当不存在Auth.User cookie时,登录系统/ auth / redirect等可以正常工作
如果有人可以帮助我解决这个问题,我将不胜感激
谢谢
解决方法
我没有尝试过,但是根据手册,您必须将autoRedirect设置为false,如下所示:
function beforeFilter() {
$this->Auth->autoRedirect = false;
}
然后session-> delete而不是session-> destroy,session-> delete清除数据,session-> destroy销毁会话和cookie并创建一个新的会话和cookie,这就是您所要处理的
,永远不要在Cookie中保存密码
您应该保存md5(username)而不是将所有登录数据保存到数据库中。