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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.25k
    Comment on it

    In a portal login page once you check the Remember Me checkbox your login id will be remembered on the browser for 7 days. And when with in a 7 days if you login on the same browser, you will get logged out.

    Use the below code in order to use it in your application.

    In your user controller:

    public function beforeFilter() {
        $this->Auth->allow(array('login', 'register'));
        parent::beforeFilter();
    }
    
    public function login() {
        if ($this->request->is('post')) {
    
            if ($this->Auth->login()) {
    
                // did they 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, '2 weeks');
                }
    
                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());
    }
    

    In the login view:

    Login

    <?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'); ?>
    

    In your AppController:

    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~&#95;+!@#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: