Cookies in php is defined in the following line of code, CookieComponent is a wrapper in a PHP setcookie method.It offers user a number of methods for working with the Cookies in the document, cookies will allow user to remember their username and password when a user want to login himself in the same field, cookies will allow user to store the previous detail to it and they can be able to use the detail as it was saved in cookies.
class RememberMeComponent extends Object
{
var $components = array('Auth', 'Cookie');
var $controller = null;
/**
* Cookie retention period.
*
* @var string
*/
var $period = '+2 weeks';
var $cookieName = 'User';
function startup(&$controller)
{
$this->controller =& $controller;
}
function remember($username, $password)
{
$cookie = array();
$cookie[$this->Auth->fields['username']] = $username;
$cookie[$this->Auth->fields['password']] = $password;
$this->Cookie->write(
$this->cookieName,
$cookie,
true,
$this->period
);
}
function check()
{
$cookie = $this->Cookie->read($this->cookieName);
if (!is_array($cookie) || $this->Auth->user())
return;
if ($this->Auth->login($cookie))
{
$this->Cookie->write(
$this->cookieName,
$cookie,
true,
$this->period
);
}
else
{
$this->delete();
}
}
function delete()
{
$this->Cookie->del($this->cookieName);
}
}
UsersController source code:
class UsersController extends AppController
{
var $name = 'Users';
var $uses = array('User');
public function login() {
if ($this->Auth->user()) {
$this->redirect(array('controller'=>'users','action'=>'home'));
}
if (($this->data['User']['remember_me'])==0)
{
$this->RememberMe->delete();
}
else{
$this->RememberMe->remember
(
$this->data['User']['email'],
$this->data['User']['password']
);
$this->redirect(array('controller'=>'users','action'=>'home'));
}
unset($this->data['User']['remember_me']);
if ($this->request->is('post')) {
if ($this->Auth->login()){
$this->redirect($this->Auth->redirectUrl());
}
else
$this->Session->setFlash('Username or password is incorrect');
}
}
function logout()
{
$this->RememberMe->delete();
$this->redirect($this->Auth->logout());
}
}
0 Comment(s)