Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • how to do save the cookies in cakephp and remember me in cakephp

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 503
    Comment on it

    In your login page you have two fields that are username/email and password in that page you have a checkbox . In that form you have a checkbox if you select that check box then your cookies will be saved in your browser for that remember me code you have to save select that checkbox for that you have to write few lines of code.


    Code for Appcontroller goes here:

    
    	public function beforeFilter() {
            // parent::beforeFilter();
               $this->Cookie->key = 'qSI232qs*&sXOw!adre@34SAv!@*(XSL#$%)asGb$@11~_+!@#HKis~#^';
        $this->Cookie->httpOnly = true;
    
        if (!$this->Auth->user() && $this->Cookie->read('done')) {
            $cookie = $this->Cookie->read('done');
            // echo debug($cookie);
    
            $user = $this->User->find('first', array(
                'conditions' => array(
                    'User.email' => $cookie['email'],
                    'User.password' => $cookie['password']
                )
            ));
    
           
        }

    you should also write this code also in your AppController:

    public $components = array(        
    		'Qimage',)

     Code for the UserController goes here:

    This is the login function in UserController

    public function login() {
    
    	 if ($this->Auth->user())
    			    {
    			        $this->redirect(array('controller'=>'users', 'action'=>'home'));
    			    }
    
        if ($this->request->is('post')) {
    
            if ($this->Auth->login()) {
    
                // did they select the remember me checkbox?
                if ($this->request->data['User']['done'] == 1) {
                    // remove "remember me checkbox"
                    unset($this->request->data['User']['done']);
    
                    // // hash the user's password
                    // $this->request->data['User']['password'] = $this->Auth->password($this->request->data['User']['password']);
    
                    // write the cookie
                    $this->Cookie->write('done', $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'
        ));
       $user = $this->Cookie->read('done');
        $this->set('user', $user);
        $this->set('check', 1);
    }
    
    
    

    This is the logout function in UserController:

       public function logout() {
    
         	  $this->Cookie->delete('remember_me_cookie');
                  
                    return $this->redirect($this->Auth->logout());
                }
    
       

    Code for the checkbox goes here:

     <?php echo $this->Form->checkbox('done' ,array('hiddenField' => true,'checked'=>$check));?>

     

 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: