Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Discussion on remember me in Cakephp

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 460
    Comment on it

    Hi Readers,
    Welcome to FindNerd,today we are going to discuss on remember me in CakePHP.

    If we have a login form in our CakePHP web application, So when we checked the “Remember Me” checkbox in our login page then our login id will be remembered on the browser for 7 days. Means when we will login with the same browser then just we have to click on login button we will get login in our application, And when we will login after 7 days on the same browser then we will get logged out.

    you can follow below step:

    Firstly let see our User controller:

    <?php
      public function beforeFilter() {
        $this->Auth->allow(array('login', 'register'));
        parent::beforeFilter();
      }
    
      public function login(){
        if ($this->request->is('post')){
          if ($this->Auth->login()) {
           //select the remember me checkbox?
            if ($this->request->data['User']['remember_me'] == 1) {
              // remove "remember me checkbox"
              unset($this->request->data['User']['remember_me']);
              $this->request->data['User']['password'] = $this->Auth->password($this->request->data['User']['password']);
              // define the cookie
              $this->Cookie->write('remember_me_cookie', $this->request->data['User'], true, '2 weeks');
            }
            return $this->redirect($this->Auth->redirect());
          }else{
             $this->Session->setFlash(_('Username or password is incorrect.'));
          }
        }
        $this->set(array('title_for_layout' => 'Login'));
      }
    
      //logout function
      public function logout(){
        //clear the cookie (if it exists) when logging out
        $this->Cookie->delete('remember_me_cookie');
        return $this->redirect($this->Auth->logout());
      }
    ?>

    Now let see login view (login.ctp)

    <?php
      <?php echo $this->Form->create('User'); ?>
      <?php echo $this->Form->input('username'); ?>
      <?php echo $this->Form->input('password'); ?>
      <?php echo $this->Form->checkbox('remember_me'); ?> Remember Me
      <?php echo $this->Form->end('Login'); ?>
    ?>

    AppController will be like below:

    <?php
      public $components = array('Session', 'Auth','Cookie');
      public $uses = array('User');
        public function beforeFilter(){
          // set cookie options
          $this->Cookie->key = 'qSI2$2qs*&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'); //after destroy session & cookie
            }
          }
        }
    ?>

    I hope this blog will help you to set “Remember Me” in your CakePHP web application for the login page.

     

 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: