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

How do I set a cookie on localhost with Phalcon?

In normal PHP, to set a cookie you need to run the following code:

bool setcookie ( string $name [, string $value = "" [, int $expire = 0 [, string $path = "" [, string $domain = "" [, bool $secure = FALSE [, bool $httponly = FALSE ]]]]]] )

And to get it to work on localhost, string $domain needs to be false.

To set a cookie with Phalcon, you need to run the following code:

public set (mixed $name, [mixed $value], [mixed $expire], [mixed $path], [mixed $secure], [mixed $domain], [mixed $httpOnly])

However, when I try and send the [mixed $domain] as false I get the following error:

Parameter 'domain' must be a string

Setting it to '', null, 'localhost', '.localhost', or '127.0.0.1' doesn't throw an error, but doesn't save the cookie.

The next thing my code does is $this->response->redirect().

How can I get cookies to work on localhost?



13.8k
edited Aug '18

On a localhost you need to add it like this to have it set.

setcookie('name', 'value', false, '/', false);

Tested it on an empty project: https://prntscr.com/kl3pnh