Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to remember username and password in cakephp using cookies

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 339
    Comment on it

    As we seen in  login page if we click or check the checkbox of  “Remember Me” then our  login id will be remembered by the browser . This is done by using cookies .

    our login form contain email field and password along with checkbox name remember me . when we submit our form if this remember me checkbox is checked then we are storing value of user in the cookie.

    Below is the Appcontroller logic.

    public function beforeFilter() {
            $this->Cookie->httpOnly = true;
            if (!$this->Auth->user() && $this->Cookie->read('rememberMe')) {
    
                $cookie = $this->Cookie->read('rememberMe');
                //checking cookie is non empty
          
               if(isset($cookie) && !empty($cookie)) {
                
                    $user = $this->User->find('first', array(
                            'conditions' => array(
                                    'User.username' => $cookie['username'],
                                    'User.password' => $cookie['password']
                            )
                    ));
                }    
            }
             
        }
    }

    In below code we are storing user information to cookie if remember me checkbox  is checked .We are setting 1 week as cookie expiration time, so user can be logged in for 1 week.

    Below is the user controller logic.

    
    function login(){
        $this->layout="main";
    
        if ($this->Auth->user())
        {
            $this->redirect(array('controller'=>'users', 'action'=>'home'));
        }
       
       if ($this->request->is('post')){
       
             if ($this->Auth->login()){
               
                if ($this->request->data['User']['rememberMe'] == 1) {
                                        // After what time frame should the cookie expire
                    $cookieTime = "1 week"; // You can do e.g: 1 week, 17 weeks, 14 days
    
                    // remove "remember me checkbox"
                    unset($this->request->data['User']['rememberMe']);
    
                    // write the cookie
                   
                    $cookie = $this->request->data['User'];
    
                    $this->Cookie->write('rememberMe', $cookie, true, $cookieTime);
    
                }
                        
                $this->redirect($this->Auth->redirectUrl());
            }
        
            $this->Flash->error(__('Invalid username or password, try again'));
        }
        $user = $this->Cookie->read('rememberMe');
        $this->set('user', $user); //passing username and password to view
        $this->set('checked', 1);
    
    }

     

     

 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: