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

Cookies

What is the correct way to set cookies from controller?

looking at the source code ,it must be something like this, but won't work.

$cook = new \Phalcon\Http\Response\Cookies();
$cook->setDI($this->getDI());
$cook->set('testook', 'testvalue', time()+60*60*24, '/');

and going through the whole source from $app->handle(), at the end there is call

PHALCON_CALL_METHOD_NORETURN(response, "sendheaders");

and $response->sendHeaders() call's send() only if $response->_headers != null

and then in $response->send()

PHALCON_OBS_VAR(cookies);
phalcon_read_property(&cookies, this_ptr, SL("_cookies"), PH_NOISY_CC);
if (Z_TYPE_P(cookies) != IS_NULL) {
    PHALCON_CALL_METHOD_NORETURN(cookies, "send");
}

so $response->_cookies is internally set from $cook->set(...) call, BUT

PHALCON_CALL_METHOD_NORETURN(cookies, "send");

There is no send method in \Phalcon\Http\Response\Cookies :/ I've missed something?



98.9k

Response\Cookies component still need some more work, it's simpler use the set_cookie function provided by PHP:

setcookie('testook', 'testvalue', time()+60*60*24, '/');


98.9k

The cookies component is now ready to use in 1.1.0



19.2k
edited Jul '14

@phalcon I'm using 1.2.0 and cookies expire time just don't get set:

$this->cookies->set('sample_cookie', 'sample_value', time() + 86400 * 7);

sets a session cookie.

However if I'm using:

setcookie('sample_cookie', 'sample_value', time() + 86400 * 7);

it works fine.

Tried Vokuro app and this issue persists there.