Remember Me using Cookie
We can remember username and password by using Cookie. As we know cookie resides in a browser and used to store user data and other informations.
We will remember username and password by using cookie when user check the remember me checkbox and saves the information in a cookie variable.
Controller Logic:
public function login()
{
if ($this->request->is('post')) {
if ($this->Auth->login()) {
if ($this->request->data['User']['remember_me'] == 1) {
$this->Cookie->write('remember_me',$this->request->data['User'], true, 2000);
}
if ($this->request->data['User']['remember_me'] == 0) {
$this->Cookie->destroy('remember_me');
}
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error(__('Invalid username or password, try again'));
}
//send username and password in $cook variable to view
$cook=$this->Cookie->read("remember_me");
$this->set('user',$cook);
}
We will use the $cook variable value in view to auto fill the username and password (if remember_me checkbox is checked) when the user logout.
0 Comment(s)