Cakephp 3.x记住我登录功能
我正在用CakePHP3.x开发我的网站。在这方面,我想记住我登录用户的功能 我在谷歌上做了很多研究,但我找不到任何对我有帮助的东西。 我已经试着申请了 但这对我毫无帮助 我的数据库中有用户名作为电子邮件和密码作为密码字段 如果有人有任何想法,请与我分享 我的登录功能:Cakephp 3.x记住我登录功能,php,cakephp,authentication,cookies,cakephp-3.0,Php,Cakephp,Authentication,Cookies,Cakephp 3.0,我正在用CakePHP3.x开发我的网站。在这方面,我想记住我登录用户的功能 我在谷歌上做了很多研究,但我找不到任何对我有帮助的东西。 我已经试着申请了 但这对我毫无帮助 我的数据库中有用户名作为电子邮件和密码作为密码字段 如果有人有任何想法,请与我分享 我的登录功能: $user = $this->Auth->identify(); if ($user) { $this->Auth->setUser($user); i
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
if ($this->request->data['rememberMe'] == "on") {
$cookie = array();
$cookie['username'] = $this->request->data['email'];
$cookie['password'] = $this->request->data['password'];
$this->Cookie->write('rememberMe', $cookie, true, "1 week");
unset($this->request->data['rememberMe']);
}
if (empty($this->data)) {
$cookie = $this->Cookie->read('rememberMe');
if (!is_null($cookie)) {
$user = $this->Auth->identify();
if ($user) {
$this->redirect($this->Auth->redirectUrl());
} else {
$this->Cookie->destroy('rememberMe'); # delete invalid cookie
$this->Session->setFlash('Invalid cookie');
$this->redirect('login');
}
}
}
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->set('Invalid username or password, try again', ['element' => 'error']);
}
编写“记住我”cookie的最简单方法是使用此插件中的身份验证适配器: 之后
if (!is_null($cookie)) {
加
这应该行得通
Auth组件正在使用$this->request->data进行身份验证。添加cookie数据后,identify()应该可以工作。我在我的网站上使用了类似的代码,没有任何问题。从您的搜索中,您尝试过什么吗?如果是这样,有什么不起作用?您应该编辑您的问题并添加更多信息。谢谢修复拼写和英语一个小问题被编辑,添加了我的逻辑这起作用了,但它还需要在
if($user){
添加$this->Auth->setUser($user);
之后再次添加。将会话cookie的用户名和密码存储在不安全的环境中。请参阅另一种选择
$this->request->data = $cookie;