hackedhacker
(Twister Thins Strawberry)
1
How do I keep the cookie after the browser or even the computer restart?
I somehow is able to keep it even after I logout my user.
But i want to keep the value even after the browser or even the computer is closed and restarted.
Is it possible?
I’m not keeping any sensitive information, and I wanna keep it on client-side.
And all those information would be re-validated on server again before re-use.
Is it suppose to be done automatically and just somehow my code doesn’t work that way?
Or am I missing something?
The code I use
// adding the cookie
$cookie = ['data1'=>'testing', 'data2'=>'test again'];
\Yii::$app->response->cookies->add(new \yii\web\Cookie(['name'=>'someKey', 'value'=>$cookie]));
// ...
//removing the cookie (only trigger if clicked a button provided)
\Yii::$app->response->cookies->remove('someKey');
jacmoe
(Jacob Moena)
2
hackedhacker
(Twister Thins Strawberry)
3
Thanks.
Missed that earlier.
But somehow, when I print the cookie object, the expire value is not show even after the result turns out as expected.
// adding the cookie
$cookie = ['data1'=>'testing', 'data2'=>'test again'];
\Yii::$app->response->cookies->add(
new \yii\web\Cookie([
'name'=>'someKey',
'value'=>$cookie,
'expire'=>(time() + (60*60*24*30))
])
);
// trying to print the cookie on view
<pre>
<?php $cookie = \Yii::$app->request->cookies->get('someKey'); ?>
<?=print_r($cookie)?>
</pre>
// the result
yii\web\Cookie Object
(
[name] => someKey
[value] => Array
(
[data1] => testing
[data2] => test again
)
[domain] =>
[expire] =>
[path] => /
[secure] =>
[httpOnly] => 1
)
Although my earlier problem is solved. This is just a minor follow up question, I believe I won’t change my code for this part anymore anyway.
Is the expire attribute suppose to not be shown there?
But I believe the expire does works, because they cookie stay and is accessible even after I restart the browser now.
jacmoe
(Jacob Moena)
4
Use your browser to see what cookies have been set for which domain, and examine them.
You should be able to see the expiration date-time there. 