Reputation: 1291
How to destroy cakephp cookies on page refresh or when I close the browser window?
my code: merry_parents_controller.php
$this->Cookie->write('MerryParent.id',$this->MerryParent->id,false,0);
echo 'cookie MerryParent.id: '.$this->Cookie->read('MerryParent.id');
$this->set('id',$this->Cookie->read('MerryParent.id'));
thanks.
Upvotes: 1
Views: 2238
Reputation: 520
It seems to me that you want to imitate the behavior of flash messages. If that's right, you may be interested in this part of SessionHelper source code.
If I should simplify it for you, it would look like this (in a controller):
$key = 'MerryParent.id';
$value = '';
if ($this->Session->check($key)) {
$value = $this->Session->read($key);
$this->Session->delete($key);
}
If this is not any helpful, please describe more of what you're trying to accomplish. Maybe there is a better way.
Upvotes: 1
Reputation: 5420
Using 0 as the last parameter will mean the cookie will be deleted when the session finishes (browser is closed).
i.e, $this->cookie->time = 0;
If you want to destroy cookies all the time (no idea why), add functionality to do so in your AppController's beforeFilter()
, i.e $this->cookie->delete('MerryParent');
(will delete entire MerryParent key).
Upvotes: 1