Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • cakePHP remember me login using Auth

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.13k
    Comment on it

    If you select the tick in the ‘remember me’ box when you login in any web page with your Username and password, your login credentials will be saved in a cookie on this computer. This means that whenever the next time you log on to the same website , you will not need to re-enter the login details again.

    But if we delete the cookie from our browser or browser do not accept cookies this 'Remember Me' function will not work.

    You can use the Remember me function in cakephp by using some codes. As its a important part in login function.You can write a few lines code under the login functions of cakePHP . In this code we have only defined that if the user selects the remember me box then the cookies will be saved in user's browser and if he logs out, the cookies will be cleared.

    // login function
    public function login() { 
        if ($this->request->is('post')) {
    
            if ($this->Auth->login()) {
    
                // if user select the remember me checkbox
    
                if ($this->request->data['User']['remember_me'] == 1) {
                   
     // remove "remember me checkbox"
                    unset($this->request->data['User']['remember_me']);
    
                    // hash the user's password
                    $this->request->data['User']['password'] = $this->Auth->password($this->request->data['User']['password']);
    
                    // write the cookie
                    $this->Cookie->write('remember_me_cookie', $this->request->data['User'], true, '1 weeks'); //if user select remember me, the cookie will be saved in his browser for 1 week.
                }
    
                return $this->redirect($this->Auth->redirect());
    
            } else {
                $this->Session->setFlash(__('Username or password is incorrect.'));
            }
        }
    
        $this->set(array(
            'title_for_layout' => 'Login'
        ));
    }
    
    public function logout() {
    
        // clear the cookie (if it exists) when logging out
        $this->Cookie->delete('remember_me_cookie');
    
        return $this->redirect($this->Auth->logout());
    }

    After adding the function , you can call the Remember me check box in login.ctp files under View folder

        <?php echo $this->Form->checkbox('remember_me'); ?> Remember Me

    After calling the code we also have to define the cookie component in app controller. In app controller we have to see cookie when user login and to destroy the cookie when user logs out .

     

    public $components = array(
        'Session',
        'Auth',
        'Cookie'
    );
    
    public $uses = array('User');
    
    public function beforeFilter() {
        
    // set cookie options
        $this->Cookie->key = 'qSI232qs*&sXOw!adre@34SAv!@*(XSL#$%)asGb$@11~_+!@#HKis~#^';
        $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']
                )
            ));
    
            if ($user && !$this->Auth->login($user['User'])) {
                $this->redirect('/users/logout'); // destroy session & cookie
            }
        }
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: