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 being stored in session

Hi, i currently have Phalcon 3.2.4 and cookies are being stored in session, when user close the browser the cookie dissapears, i dont know what im doing wrong but this didnt happen on Phalcon 2.X

My Code:

$di->setShared('cookies', function () {
    $cookies = new Cookies();

    return $cookies;
});
$this->cookies->set(
'phalcon',
'some value phalcon',
time() + 3600,
'/'
);


4.8k
Accepted
answer
edited Nov '17

Are you sure Phalcon is setting any headers to begin with?

In other words:

$application = new \Phalcon\Mvc\Application($di);
$response = $application->handle();
$response->send();

When you use $response->send(); this both sets headers and cookies (which Phalcon tracks separately) and echos HTML.
A common mistake is to use echo $response->getContent(); instead.

See: https://github.com/phalcon/cphalcon/blob/master/phalcon/http/response.zep#L606-L634

edited Nov '17

Oh, i see if i use die() at end, it doesn't save the cookie but it stills save it as a session, my fault.

I was doing this:

        $this->cookies->set(
            'phalcon',
            'some value phalcon',
            time() + 3600,
            '/'
        );

        if($this->cookies->has("phalcon")){
            echo $this->cookies->get("phalcon")."<br>";
        }

        die("END");