I'm wondering how to set username & password in a cookie. I have a 'remember me' checkbox. Essentially what i want is: If a user checks the 'remember me' checkbox, after logout the username, password and checkbox should be filled in.
UsersController
public function login() {
// used to check session is exist or not.if yes then redirect to index page
$SessionData = $this->Session->read('username');
if (!empty($SessionData))
{
$this->redirect(array('controller' => 'users', 'action' => 'index'));
}
// session check ends here
if (!empty($this->request->data))
{
$this->User->set($this->request->data);
if($this->User->validates(array('fieldList' => array('username','password'))))
{
$user = $this->User->find('first', array(
'conditions' => array(
'User.username' => $this->request->data['User']['username'],
'User.password' => md5($this->request->data['User']['password'])
)
));
// used to write username in session
$this->Session->write("username", $user['User']['username']);
//remember username and password code
if (!empty($SessionData))
{
if(isset($this->request->data['User']['remember_me']) && $this->request->data['User']['remember_me'] == 1)
{
$this->Cookie->write('SPLoginUser', $user);
}
if (!empty($SPLoginUser))
{
$email = $this->Cookie->read('SPLoginUser');
if (!is_null($email))
{
$remember_me = 1;
$this->request->data['User']['username'] = $email;
}
}
}
}
}
}
Login.ctp
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create(array('controller'=>'Users','action'=>'login','method'=>'POST')); ?>
<?php echo $this->Form->input('username'); ?>
<?php echo $this->Form->input('password'); ?>
<?php echo $this->Form->checkbox('remember_me', array('hiddenField' => false, 'value' => '1')); ?> Remember me
<?php echo $this->Form->end(__('Login')); ?>