Skip to main content
Share Your Experience: Take the 2024 Developer Survey
27 votes

How do I set the cookie lifetime?

For the session cookie options D8 uses container parameters instead of settings. Create a services.yml file in the same folder as settings.php. The default values are in default.services.yml. You can ...
4uk4's user avatar
  • 100k
18 votes
Accepted

How to setup cookie domain?

This is a container parameter now. Put this in services.yml or another service definition YAML file: parameters: session.storage.options: cookie_domain: '.domain.com' Since it is in a yaml ...
Berdir's user avatar
  • 83.3k
13 votes

How do I set a cookie?

With Drupal 7, Drupal 8, and Drupal 9 you could use user_cookie_save() to store a cookie. function user_cookie_save(array $values) { foreach ($values as $field => $value) { // Set cookie for ...
apaderno's user avatar
  • 97.5k
8 votes
Accepted

What is the difference between a Drupal session variable and a cookie?

Seeing this has two votes, I'll try to explain it. Drupal uses the built-in PHP session mechanism, which as you rightly suspect is based on a cookie. The cookie name is SESS followed by a long string ...
Alfred Armstrong's user avatar
7 votes
Accepted

How does drupal set user cookie

The following code, let's login any user programmatically. Not exactly; it sets the current global user object to an instance of a user, but it doesn't log the account in or touch the session. Many ...
Clive's user avatar
  • 167k
7 votes
Accepted

Can't find cookie for validation in EventSubscriber

Some ideas: Like for the config you have to add a cache dependency for the cookie, too: $response->getCacheableMetadata()->addCacheContexts(['cookies:' . $config_cookie]); Or use an ...
4uk4's user avatar
  • 100k
7 votes
Accepted

Cookie Compliance and Google Analytics

From the EU Cookie Compliance module's project page on drupal.org: If you want to conditionally set cookies in your module, there is a JavaScript function provided that returns TRUE if the user has ...
hotwebmatter's user avatar
  • 2,112
6 votes
Accepted

Authenticate using OAuth

I think you're desire to use Oauth is justified. However, if you have no other options, you can use Cookie authentication. I have used it before and have heard of people using it as well. Here are ...
Jigarius's user avatar
  • 3,119
6 votes

How do I set a cookie?

Drupal 8 cookies can be set using ResponseHeaderBag from the Symfony\Component\HttpFoundation\Response object. Set new cookie value use Symfony\Component\HttpFoundation\Cookie; $cookie = new use ...
bpile's user avatar
  • 288
5 votes
Accepted

Create cookie on login

You could just write the code yourself in a custom module. What you want is pretty simple. /** * Implements hook_user_login(). */ function HOOK_user_login(&$edit, $account) { setcookie('user-...
googletorp's user avatar
  • 35.4k
5 votes
Accepted

How to correctly use cookies? (Twig and cache context)

I've updated the code in the question, the code is now working. It was almost complete, I had made some simple mistakes though: Even when providing a proper cache context, you still need to disable ...
Hudri's user avatar
  • 5,539
5 votes
Accepted

How do I set a cookie using $response->headers->setCookie() in a form submission handler?

You can't send a response from custom code in Drupal. You have to return it from a controller. You can also set a response or add the cookie to an existing response in an event subscriber. In your ...
4uk4's user avatar
  • 100k
4 votes

Headless Drupal 8 - cookie authentication

the 403 error comes with a message saying "Authentication method not allowed on this route" OK, so it appears that route may not support the cookie authentication method, by default. For example, ...
David Thomas's user avatar
  • 21.9k
4 votes

Headless Drupal 8 - cookie authentication

It looks like maybe you are not successfully retrieving the CSRF token. The CSRF token can be retrieved from /rest/session/token. See https://www.drupal.org/docs/8/core/modules/rest/3-post-for-...
edwardchiapet's user avatar
4 votes
Accepted

How do I delete cookies when users log out?

Imlement hook_user_logout($account) and call user_cookie_delete($cookie_name). For example: function mymodule_user_logout($account) { user_cookie_delete('name'); user_cookie_delete('mail'); ...
Wim Mostrey's user avatar
  • 2,827
4 votes

Multi-domain cookie

A cookie can only have 1 domain value. So you can either set it to a specific domain or the top domain wich includes all the subdomains. See https://stackoverflow.com/questions/1062963/how-do-browser-...
h3rj4n's user avatar
  • 181
4 votes
Accepted

Invalidate Internal Page Cache on cookie change

You have to override the PageCache::getCacheId method by adding the cookie value to form the Cache ID. Have a look at Page Cache by cookies example.
hugronaphor's user avatar
  • 1,462
4 votes

Can't find cookie for validation in EventSubscriber

Original splash_redirect module dev here. Thanks for joining me in the caching rabbit hole! (Sorry, can't make 'comments' yet so I have to put this in an answer) Thanks to @4k4 for the great ...
AdamB's user avatar
  • 164
4 votes
Accepted

How do I dynamically change cookie_lifetime?

You can override SessionConfiguration::getOptions to set the cookie lifetime dynamically: /mymodule/src/MySessionConfiguration.php: <?php namespace Drupal\mymodule; use Drupal\Core\Session\...
4uk4's user avatar
  • 100k
4 votes
Accepted

Cookie sharing on multidomain site

This is not really a Drupal question, but a browser question. The cookies a browser has for example.com will never be sent to example.org. You can look at other ways to create federated login, but ...
greggles's user avatar
  • 5,242
4 votes

Cookie Compliance and Google Analytics

There is a new module for Drupal 8 and 9: COOKiES. It supports the modules Google Analytics and Google Tag Manager (and more). Maybe that's what you are looking for. COOKiES User consent management ...
Joachim Feltkamp's user avatar
4 votes

How can I read a cookie I programmatically set?

Oh... thanks cilefen. That was a good question that helped me find the problem :-) I was writing down the attributes to post in reply when I realised my cookie domain differed from the Drupal session ...
BrettSh's user avatar
  • 109
3 votes

Invalidate Internal Page Cache on cookie change

The Internal Page Cache doesn't support cache contexts. It is a simple reverse proxy designed to serve static database content and the only Drupal specific feature it has is to invalidate cache tags. ...
4uk4's user avatar
  • 100k
3 votes
Accepted

WSOD when redirecting based on cookie

You should not be using theme_preprocess to perform any processing the results of which cannot be cached, only manipulation of variables to be passed to the final theme processing function or template....
Alfred Armstrong's user avatar
3 votes

Sessions / Cookies not working for user login, registration or shopping cart

This is a very old question, but I stumbled on it while having the same issue. In my case, the "rewrite" Apache module was turned off. I re-enabled it with: a2enmod rewrite I could access /user, ...
bgm's user avatar
  • 724
3 votes

Users can't log in sometimes

I believe there is some kind of caching in play here. I've found that I can successfully log in with the demo user provided by generating unique login URLs. Each unique login URL I create has worked ...
adfaklsdjf's user avatar
3 votes

Users can't log in sometimes

That can happen if the site is redirecting to a different domain or to a secure/non-secure protocol upon login. I.e. with/without www or to http/https. If so, you could set a base domain and cookie ...
dxc's user avatar
  • 2,676
3 votes
Accepted

Rest login cookie token explanation

You are logging in correctly by POSTing the username and password. After you've logged in, a Drupal session is created. The csrf_token can be used to authorise future requests requests by sending it ...
imclean's user avatar
  • 1,076
3 votes

Setting SameSite=None on a session cookie

You can change the session cookie settings in services.yml. See How do I set the cookie lifetime? This is not only possible for the default parameters defined by Drupal but for any parameter listed in ...
4uk4's user avatar
  • 100k

Only top scored, non community-wiki answers of a minimum length are eligible