We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Class Phalcon\Http\Response\Cookies not work

Class Phalcon\Http\Response\Cookies method: set() is not always working. When using php setcookie I don't have problems

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.



31.9k
edited Mar '14

My phalcon version is 1.2.6, But this issue is still not solved.. The code is like this: $this->cookies->set('captcha', 'a', time() + 86400, '/');



31.9k
edited Mar '14

It's still not working

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.

This cookies set is not working on my application though.... :( I don't know why..

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;
        });


2.8k

$this->cookies->set('gift_order','2221dddd',time()+86400); $this->cookies->send();



20.4k

$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 :)