记住我使用cakephp3

时间:2016-03-03 23:02:18

标签: login cakephp-3.x

执行此代码时出错 我的错误是在布尔上调用成员函数read()  这是函数登录的代码:

 public function login()
{
    if ($this->request->is('post')) {
        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);
            if ($this->request->data['rememberMe']=="on") {

                $cookie = [];
                $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)) {
                    $this->request->data = $cookie;
                    $user = $this->Auth->identify();
                    if ($user) {
                        $this->Auth->setUser($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());
        }
        else
        $this->Flash->error(__("Nom d'utilisateur ou mot de passe incorrect, essayez de nouveau."));
    }
}

我更改了data['rememberMe']=="on"数据[' rememberMe'] == 1` 显示了另一个错误在布尔

上调用成员函数write()

2 个答案:

答案 0 :(得分:0)

您好@Ikbel尝试在IPAll中添加Cookie组件吗?

AppController.php

Doc:Cookie — CakePHP Cookbook 3.x documentation

如果在您的模板登录中使用复选框,请将 public function initialize() { parent::initialize(); $this->loadComponent('RequestHandler'); $this->loadComponent('Flash'); $this->loadComponent('Cookie'); } 替换为$this->request->data['rememberMe']=="on"

$this->request->data['rememberMe'] == 1替换为$this->Cookie->destroy

您的错误: 在boolean'$ this-> Session-> setFlash('Invalid cookie')上调用成员函数setFlash();'

替换为:$ this-> Flash->错误(('无效的Cookie')); $ this-> Flash->错误((“用户名或密码不正确,请重试。”));

答案 1 :(得分:0)

 public function login()
{
    if ($this->request->is('post')) {
        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);
            if ($this->request->data['xx']==1) {


                $username= $this->request->data['email'];
                $password = $this->request->data['password'];
                $this->Cookie->write('username', $username, true);
                $this->Cookie->write('password', $password, true);
                unset($this->request->data['xx']);
            }
            if (empty($this->data)) {
                $username = $this->Cookie->read('username');
                $password=$this->Cookie->read('password');
                if ((!empty($password))&&(!empty($username))) {
                    $this->request->data['email']=$username;
                    $this->request->data['password']=$password;
                    $user = $this->Auth->identify();
                    if ($user) {
                        $this->Auth->setUser($user);
                        $this->redirect($this->Auth->redirectUrl());
                    } else {
                        $this->Cookie->delete('xx'); # delete invalid cookie

                        $this->Flash->error(_('Invalid cookie'));
                        $this->redirect('/users/login');
                    }
                }
            }


            return $this->redirect($this->Auth->redirectUrl());
        }
        else
        $this->Flash->error(__("Nom d'utilisateur ou mot de passe incorrect, essayez de nouveau."));
    }
}
login.ctp <?php echo $this->Form->checkbox('xx'); ?> Remember Me中的

数据存储在cookie中,但是当我登录时,他没有以登录形式写入电子邮件和密码 enter image description here