记住我的功能CakePHP

时间:2016-04-22 12:48:26

标签: cakephp cookies

我想知道如何设置用户名&密码在cookie中。 我有一个'记住我'复选框。基本上我想要的是:如果用户检查“记得我”。注销后,应填写用户名,密码和复选框。

UsersController

public function login() {  

    // used to check session is exist or not.if yes then redirect to index page

    $SessionData = $this->Session->read('username');

    if (!empty($SessionData))
    {
        $this->redirect(array('controller' => 'users', 'action' => 'index'));
    } 

    // session check ends here
    if (!empty($this->request->data))
    {         
        $this->User->set($this->request->data); 
        if($this->User->validates(array('fieldList' => array('username','password'))))
        {    
            $user = $this->User->find('first', array(
                'conditions' => array(
                    'User.username' => $this->request->data['User']['username'],
                    'User.password' => md5($this->request->data['User']['password'])                                            
                )
            )); 
            // used to write username in  session 
            $this->Session->write("username", $user['User']['username']);

            //remember username and password code           
            if (!empty($SessionData))
            {          
                if(isset($this->request->data['User']['remember_me']) && $this->request->data['User']['remember_me'] == 1)
                {
                    $this->Cookie->write('SPLoginUser', $user);
                } 
                if (!empty($SPLoginUser))
                {
                    $email = $this->Cookie->read('SPLoginUser'); 
                    if (!is_null($email))
                    {              
                        $remember_me = 1;
                        $this->request->data['User']['username'] = $email;

                    }
                }
            }
        }
    }
}

Login.ctp

<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create(array('controller'=>'Users','action'=>'login','method'=>'POST')); ?>  
<?php echo $this->Form->input('username'); ?>
<?php echo $this->Form->input('password'); ?>
<?php echo $this->Form->checkbox('remember_me', array('hiddenField' => false, 'value' => '1')); ?> Remember me   
<?php echo $this->Form->end(__('Login')); ?> 

0 个答案:

没有答案