Class Phalcon\Http\Response\Cookies method: set() is not always working. When using php setcookie I don't have problems
|
Feb '16 |
7 |
2621 |
0 |
Could you provide some code on which you have this issue? Phalcon\Http\Response\Cookies had some issues in the past (like in this post): https://forum.phalcon.io/discussion/71/cookies but as Phalcon wrote, they where fixed.
I found this: https://github.com/phalcon/cphalcon/issues/871 Maybe it is still an issue on windows (if you are developing on win) and it's easier to stay with php setcookie.
Tried this code and it worked:
// index.php
// Cookies
$di->set('cookies', function () {
$cookies = new Phalcon\Http\Response\Cookies();
$cookies->useEncryption(false);
return $cookies;
});
// IndexController
public function indexAction()
{
$this->cookies->set('gift_order','2221dddd',time()+86400,'/');
if ($this->cookies->has('gift_order')) {
$giftOrder = $this->cookies->get('gift_order');
$value = $giftOrder->getValue();
echo $value;
die;
}
}
@gk-liao , try changing this code:
$di->set('cookies', function () {
$cookies = new Phalcon\Http\Response\Cookies();
$cookies->useEncryption(false);
return $cookies;
});
into
$di->set('cookies', function () {
$cookies = new Phalcon\Http\Response\Cookies();
$cookies->useEncryption(false);
return $cookies;
}, true);
I had a similar problem when trying to set multiple cookies and only the last cookie was actually set.
Hi
I used this solution ( https://forum.phalcon.io/discussion/1944/cookies-won-t-set-unless-i-disable-encryption#C6872 ) and worked for me :)
$di->setShared('crypt', function() use($di) {
$crypt = new \Phalcon\Crypt();
$crypt->setMode(MCRYPT_MODE_CFB);
$crypt->setKey('ReallyRandomKey');
return $crypt;
});
$this->cookies->set('gift_order','2221dddd',time()+86400); $this->cookies->send();
I just want to note here, that uuking's answer fixed an issue I was having when setting a cookie via an ajax request. Adding $this->cookie->send();
fixed the issue. Thanks for that :)