Retrieve cookie using get_cookie()

Hi, I’m trying to retrieve a cookie using this Grav Method:

I tried:
{% set foo = get_cookie('MY_COOKIE') %}
<p>var: {{foo}}</p>

But it’s not working. Is this function retrieve a cookie from the header response of the page or a cookie previously set by the front-end (JS) for example?

Basically I need to read some cookies from a response header OR to read headers directly, is there a way to accomplish that?


You need to be sure that the cookie was actually set. Also, try using dump() in the twig to print stuff to the debug bar, helps a lot while debugging.

The cookie is set but I think the get_cookie() method isn’t working as using dump()always returns null, even if the cookie is set.

I just tried, it works to me. You can check the implementation of get_cookie in to see why it’s not giving you the cookie value.

As written in Twitter, to me it works, with a simple numeric cookie. Could it be that the implementation of get_cookie as linked above is filtering it? Post more information, what’s the content of the cookie, how are you calling it in your twig?

Hi, I’m posting here two screenshots, first is the cookie value and second is how I’m trying to get the value inside the twig template.

Screen Shot 2016-06-29 at 19

Screen Shot 2016-06-29 at 19

This cookie (BUSUU_USER_ID) is setup using JavaScript on the first load.

Thanks for your help Flavio.

I was testing on Grav 1.1.

I missed the fact that the get_cookie Twig filter was introduced in Grav 1.1 (, so if you’re using Grav 1.0.10 it’s not working.

P.S. Grav 1.1 has solved many issues and it’s nearly ready to be released so please use that.

Great thanks, I’ll try asap :slight_smile:

@flaviocopes7d works perfectly in Grav 1.1, thanks