CakePhp记得我的选择

时间:2016-06-21 13:56:36

标签: php cakephp cakephp-2.4

我正在尝试在cakePhp应用中启用“记住我”选项。我用这个CakePHP remember me with Auth答案来实现它。 所以看看我的UsersController:

if ( (int)$this->request->data['User']['remember_me'] == 1 ) {
                // remove "remember me checkbox"
                unset($this->request->data['User']['remember_me']);     

                $this->request->data['User']['pw2'] = $this->request->data['User']['password'];

                // write the cookie
                $this->Cookie->write('remember_me_cookie', $this->request->data['User'], true, '2 weeks');
            }

这是Model的逻辑:

$this->Cookie->httpOnly = true;

        if (!$this->Auth->loggedIn() && $this->Cookie->read('remember_me_cookie')) {
            $cookie = $this->Cookie->read('remember_me_cookie');
            $user = $this->User->find('first', array(
                    'conditions' => array(
                            'User.username' => $cookie['username'],
                            'User.password' => $cookie['password'],
                    )
            ));


    }

这是观点(我知道它并不完美)

echo $this->Form->checkbox('remember_me').'Remember me';

但请记住我的功能不起作用。可能有什么不对?

1 个答案:

答案 0 :(得分:-1)

将此用于登录和remember_me功能

 if ($this->request->is('post')) {
                if ($this->Auth->login()) {
                    if (isset($this->request->data['User']['remember_me']) && $this->request->data['User']['remember_me'] == 1) {
                        $this->Cookie->delete('Auth');
                        $cookie = array();
                        $cookie['email'] = $this->request->data['User']['email'];
                        $cookie['password'] = $this->request->data['User']['password'];
                        $this->Cookie->write('Auth.User', $cookie, true, '+2 weeks');
                        unset($this->request->data['User']['remember_me']);
                    } else {
                        $this->Cookie->delete('Auth');
                    }
                    $this->redirect($this->Auth->redirect());
                } else {
                    $this->Session->setFlash(__('Invalid email or password.'), 'default', array('class' => 'error'));
                }
            }
            $cookie = $this->Cookie->read('Auth');
            if (isset($cookie) && !empty($cookie)) {
                $this->request->data['User']['email'] = $cookie['User']['email'];
                $this->request->data['User']['password'] = $cookie['User']['password'];
                $this->request->data['User']['remember_me'] = 1;
            }