I am new in cakephp 3.0. i have successfull created a cookie but i want to destroy that cookie after one minute. i have done so far:-
public function register_cookie(){
$data = "Hello world!";
$this->Cookie->write('dataFetch', $data, true, time() + (60 * 1));
}
public function getcookie() {
$cookiedata = $this->Cookie->read('dataFetch');
echo $cookiedata;
}
but when i come after one minute in getCookie function it still prints i.e. "Hello World" i want after one minute cookie is expired. Thanks in advance :)
In cakephp 3.x as the document says you can have these paramenters
CookieComponent::write(mixed $key, mixed $value = null)
But in cakephp 2.x it use to take these parameters
To set expiry time you have to set the configuration like this
$this->Cookie->config([
'expires' => '+10 days',
]);
So you code will be like this
public function register_cookie(){
$this->Cookie->config([
'expires' => '+1 minute',
]);
$this->Cookie->configKey('dataFetch', 'encryption', false);
$data = "Hello world!";
$this->Cookie->write('dataFetch', $data);
}
First, you should check if the cookie expiration date was set correctly. For example, in Chrome (after inspect element triggers the console bar), go to Application\Storage\Cookies\Localhost and check the cookie.
In cakephp you can delete a cookie with
$this->Cookie->delete('bar');
Also you can destroy a cookie by setting the expiration date into the past with time()-1