Skip to main content
How to SET & GET Cookies in Pantheon

In most application we use Cookies to store & fetch values of a user specific operations or for operations that needs to be only performed after a specific interval even for anonymous users. For example :

 

  • You want to set a default language or region for both a logged In and anonymous user.
  • You want to show a survey pop-up on a page only once for a limited amount of time even for anonymous users
  • You want to store some data that is user specific but not critical and its ok to loose.

 

There are many but these are some of the scenarios where you will probably use cookies. Now, Setting & Getting a cookie value is a straight forward task.

You will simple use something like these to set cookies and their equivalent to even get cookies :

setrawcookie('my-cookie', 'truevalue', 0, '/');

OR

$_COOKIE['my-cookie'] = 'truevalue';

OR

An EventSubscriber 

$cookie = new Cookie('my-cookie', 'truevalue');

$response->headers->setCookie($cookie);

 

Now, these will work perfectly fine in you local or any environment other than Pantheon.

 

Now, why does it not work in Pantheon and how can you make it work : 

  • Pantheon Edge server is used as a CDN to serve pages, the edge server does not recognise a cookie and to read it unless its in a specified format.
  • To make it work Prefix the cookie name with the 7 capital letters STYXKEY_ and followed by your given name.

setrawcookie('STYXKEY_my-cookie', 'truevalue', 0, '/');

 

 

Published on

Blog type