Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cakephp 3.x记住我登录功能_Php_Cakephp_Authentication_Cookies_Cakephp 3.0 - Fatal编程技术网

Cakephp 3.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

我正在用CakePHP3.x开发我的网站。在这方面,我想记住我登录用户的功能

我在谷歌上做了很多研究,但我找不到任何对我有帮助的东西。 我已经试着申请了

但这对我毫无帮助

我的数据库中有用户名作为电子邮件和密码作为密码字段

如果有人有任何想法,请与我分享

我的登录功能:

    $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;