【问题标题】:remember me using cakephp3记得我用 cakephp3
【发布时间】:2016-03-03 23:02:17
【问题描述】:

执行此代码时出现错误 我的错误是在布尔值上调用成员函数 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"data['rememberMe']==1` 显示另一个错误 Call to a member function write() on boolean

【问题讨论】:

    标签: login cakephp-3.x


    【解决方案1】:

    嗨@Ikbel,您尝试在AppController.php 中添加Cookie 组件?

      public function initialize()
        {
            parent::initialize();
    
            $this->loadComponent('RequestHandler');
            $this->loadComponent('Flash');
            $this->loadComponent('Cookie');
        }
    

    文档:Cookie — CakePHP Cookbook 3.x documentation

    如果在您的模板登录中使用复选框,请将 $this->request->data['rememberMe']=="on" 替换为 $this->request->data['rememberMe'] == 1

    并将$this->Cookie->destroy 替换为$this->Cookie->delete

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

    替换为:$this->Flash->error(('Invalid cookie')); $this->Flash->error(("用户名或密码错误,请重试。"));

    【讨论】:

    • 嗨 @Bed0skil 我在 AppController 中添加了 cookie 组件
    • 嗨@Ikbel,如果你现在需要使用cookies 很好,但是如果你想登录你的用户查看它$this->loadComponent('Auth');Authentication CakePHP3 它会自动创建Auth cookie。看这个完整的tutorial
    • 嗨,我的问题没有登录到我的用户,我希望当我点击复选框“记住我”时,用户名和密码存储到下一次登录
    • 您当前的错误是什么? 在布尔值上调用成员函数 write() ?
    • 错误改为Call to a member function setFlash() on boolean '$this->Session->setFlash('Invalid cookie');'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多