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
    • 224
    Comment on it

    Remember me is the functionality that people usually see in the sign in page of the website.

    What it does it keep your session continue by saving the cookies.

    We can save cookies and add the remember me functionality to sign in.

    We have to add the Cookie component in the AppController and also the Auth component.

    public $components = array(
        'Flash',
    	'Cookie',
        'Auth' => array(
            'authenticate' => array(
                'Form' => array(
                    'passwordHasher' => array(
                        'className' => 'Simple'
                    )
                )
            ),
            'loginRedirect' => array(
    				'controller' => 'users',
     				'action' => 'contact'
     				),
     			'logoutRedirect' => array(
     				'controller' => 'users',
     				'action' => 'login'
     				)
        )
    );

    Now in the Controller you have to add few code in the sign in function.

    public function login()
    	{
    		if ($this->request->is('post')) {
    if ($this->Auth->login()) {
    				$cookie = array();
    	    	$cookie['email'] = $this->data['User']['email'];
    	    	$cookie['password'] = $this->data['User']['password'];
    	    	$this->Cookie->write('Auth.User', $cookie, true, '1 hour');
    		$this->redirect(array('controller'=>'drivers','action'=>'edit',$user['User']['id']));
    				$this->redirect(array('controller'=>'users','action'=>'edit',$this->Auth->user('id')));
    			} else {
    				$this->Session->setFlash("Invalid Credentials");
    				$this->redirect(array('controller'=>'users','action'=>'login'));
    				$cookie = $this->Cookie->read('Auth.User');
    				if (!is_null($cookie)) {
    					if ($this->Auth->login($cookie)) {
                    //  Clear auth message, just in case we use it.
    						$this->Session->del('Message.auth');
    						$this->redirect($this->Auth->redirect());
                } else { // Delete invalid Cookie
                	$this->Cookie->del('Auth.User');
    			}
    		}
    	}
    }

    You can add time for what duration you want the saved cookie.

    It is 1 hour in there you can also give it in seconds and days however the need you have.

 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: