CakePHP Delete Cookie Issue
I've browsed through other people's issues similar to this, but nothing seems to be exactly like what I am experiencing. Please feel free to reference me to another article if this has been addressed before.
I have written a cookie when a user authenticates that stores some basic user info locally. When the use开发者_如何学JAVAr logs out, I am trying to delete the cookie variable, but is does not delete. If I use the destroy method, then the cookie is removed, but I am curious as to what I am doing wrong here:
Cookie is written like this and is working:
function login(){
if($this->Auth->login($this->data)){
$this->Cookie->write('User.email',$this->data['User']['email'],true, '1 day');
}
}
However, using the delete function does not work...
function logout(){
$this->Cookie->delete('User');
if($this->Auth->logout($this->data)){
//auto redirected
}
}
If I replace delete with destroy, it works. Is this not working because the cookie data is encrypted? I'm probably doign something stupid, but I can't seem to figure it out.
I'm using this cookie to persist through sessions. I only want it deleted if the user clicks a logout button.
Thanks!
Looking through the source, it looks like this is either a bug or intended behavior.
The CookieComponent class has an internal __values array that it uses to keep track of cookie information. If you call delete('User.email'), it will remove the 'User' index from the __values array, including all data under the index.
However, it will only unset the cookie named 'User'. Next time Cake fires up, it will see that a cookie named 'User.email' still exists and load it back into the __values array.
Assuming it's not intended behavior, I wrote a fix and I'll go ahead and submit it to the Cake team.
精彩评论