CakePHP Delete Cookie Issue

左心房为你撑大大i 提交于 2019-12-07 08:27:15

问题


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 user 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!


回答1:


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.



来源:https://stackoverflow.com/questions/5655255/cakephp-delete-cookie-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!