How to kill the session in cakephp
I m working on l开发者_如何学Cogin module. In logout function i worte the code as below, first kill the session,then redirect it to home page. It is working fine but session is not destroying. If anybody knows please help me Here is my code snippet for login & logout:
function login()
{
if(!empty($this->data))
{
$user1= $this->User->validateLogin($this->data['User']);
if($user1== true)
{
$this->redirect(array('action'=>'index'));
}
else
{
$this->Session->setflash('Login failed, Try again');
}
}
}
function logout()
{
$this->Session->delete('User');
$this->redirect('/users/index');
}
I got information that, I have to keep header information, below is my code snippet: $this->header("Cache-Control: no-cache, no-store, must-revalidate"); $this->header("Expires: Mon, 1 Jan 1970 00:00:00 GMT");
But m not getting in which part of my controller i should paste this code? Please help me....
Try $this->Session->destroy(); The destroy method will delete the session cookie and all session data stored in the temporary file system. check this link
You're getting redirected because it's not allowed, not because of the redirect in your logout function. You need to allow the use of the logout function too, in the beforefilter of your controller (CakePHP 2):
$this->allow('logout');
you dont want to kill the session. only to log the user out!
$this->Session->delete('Auth');
otherwise you destroy cookies and other user defined settings which would be pretty annoying (language, ...)
but the official cake way would be:
$this->redirect($this->Auth->logout());
精彩评论