Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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,
'/'
);


3.5k
Accepted
answer
edited 25d ago

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 25d ago

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