Accessing JS cookies with the Cookie class

Here’s a quick question about cookies and the Kirby Toolkit.

When I use the cookie::set method, I get a cookie value that has its checksum prepended. This is fine, if I intend to use cookie::get, which seems to be able to read and validate cookies created in this manner.

The trouble comes when I want to set a cookie with Javascript, and make it available to server-side logic. In my case, it’s dismissing a banner. Say I’m using a library to read and write cookies:

Cookie.set('banner_status', 'dismissed');

…and on the server…

cookie::get('banner_status'); # -> null

…presumably because it doesn’t pass the split-and-compare hash check within the cookie class.

Any advice? Am I missing something really basic about cookie setting/getting? Do I have to resort to an AJAX call to set cookies only on the server?

:v:

1 Like

You can access this cookie value manually:

a::get($_COOKIE, 'banner_status');

Sure— I’m OK chalking this up to a misunderstanding about the usage limitations for the cookie class. My impression was that it was basically a setter/getter for data on the super-global, as the documentation doesn’t mention the validation layer.

Thought I’d open the thread in case others are confused about it’s implementation— happy to provide some additional language in the docs, but that might be better left to the class’s authors, who could provide rationale for the design.

Thanks! Marked as solved.

2 Likes