Skip to main content
22 votes

Storing data / session for anonymous user

Late answering, but it's worth mentioning that you can use private tempstore for anonymous users, as long as the tempstore has access to an active session. To do so, you need to inject your class with ...
lyserge's user avatar
  • 333
15 votes
Accepted

Storing data / session for anonymous user

In Drupal 8 the session variables can be accessed off the request: $session = \Drupal::request()->getSession(); If you want to set something in the session, you can do the following: $profile = '...
Duncanmoo's user avatar
  • 2,951
13 votes

How do I store the values submitted in a form in the session?

In D8 the session data is provided by a symfony session object, which is attached to the request. See this comparison of D7 against D8: Drupal 7: function mymodule_session_counter_increment() { if ...
4uk4's user avatar
  • 100k
9 votes
Accepted

What is the difference between \Drupal::request()->getSession() and \Drupal::service('user.private_tempstore')?

The main difference is that session data is stored in the database table sessions and PrivateTempStore in key_value_expire. Session data is preloaded in memory, private tempstore data only when ...
4uk4's user avatar
  • 100k
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

How does Drupal verify sessions from the cookie value?

I figured it out, but if someone posts a more in-depth answer, I'll accept it. Drupal has a SessionHandler with a read function: public function read($sid) { $data = ''; if (!empty($sid)) { ...
RaisinBranCrunch's user avatar
6 votes

Storing data / session for anonymous user

A session can be anonymous (uid=0). You can simply use $_SESSION and drupal will take care of this. Store session data: $_SESSION['mymodule']['variablename'] = $tempdata; Get session data: $...
4uk4's user avatar
  • 100k
6 votes
Accepted

Difference between browser session and Drupal session

What you are describing is not Drupal session vs. Browser session. There's just one session, always managed by Drupal (partially by using Symfony code. But it's still Drupal). The (private) tempstore ...
Berdir's user avatar
  • 83.3k
5 votes
Accepted

How does core triggers cleanup of expired sessions in database?

Configuring Garbage Collection When a session opens, PHP will call the gc handler randomly according to the probability set by session.gc_probability / session.gc_divisor. For example if ...
No Sssweat's user avatar
  • 31.4k
5 votes

Is it possible to unset private_tempstore values?

I have a block that creates a private_tempstore value (which I understand works exactly like PHP sessions). No, both are for different use cases. Private tempstore is used mainly for larger ...
4uk4's user avatar
  • 100k
4 votes
Accepted

How to prevent multiple logins for administrator?

Session Limit allows to put restriction on session based on user role, it could be admin role also, with an exception that it does not restrict user with uid 1 which is kind of rescue point in case ...
arpitr's user avatar
  • 7,846
4 votes

How do I store the values submitted in a form in the session?

To temporary store data in the session, in Drupal 8.5 and higher there is the tempstore.private service that, for example, is used from the node edit form to temporary store the node preview. ...
apaderno's user avatar
  • 97.5k
4 votes

No one can login and access denied message

What worked for me was enabling and configuring the $cookie_domain variable in settings.php (because I have a site that can be accessed by 2 different domains). Pay attention to the comment before ...
Heitor Althmann'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
Accepted

Reasons for limited login duration?

TLDR Look for session.gc_maxlifetime and session.cookie_lifetime. Inspect your session cookies with browser developer tools To debug this, one can use browser developer tools to inspect cookie ...
donquixote's user avatar
  • 3,652
4 votes

Drupal 8 dropping session variables on password reset. Gives 403 Access Denied

This is an issue with symfony/http-foundation v3.4.24. See https://www.drupal.org/project/drupal/issues/3045844.
Cameron Prince's user avatar
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
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

Storing data / session for anonymous user

The $this->sessionManager->start(); was not working anymore for me because of the last update. I changed it in to $this->sessionManager->regenerate();
Dan Poenaru's user avatar
3 votes
Accepted

Php Access User Data from external script

You can following file authorize.php to do it like this: <?php use Drupal\Core\DrupalKernel; use Drupal\Core\Site\Settings; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\...
MrD's user avatar
  • 6,208
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

Is it possible to unset private_tempstore values?

delete() is the method to delete a value in the store. $session = \Drupal::service('user.private_tempstore')->get('achva_sso'); $session->delete('name'); 'name' is the value passed as first ...
apaderno's user avatar
  • 97.5k
3 votes
Accepted

How do I display username in a custom block?

You can do this in sevaral ways, one of this could be use a preprocess function. In your custom module in the .module file or your custom theme in the .theme file (recommended) write this: /** * ...
Diosbel Mezquía's user avatar
3 votes
Accepted

Using a session for anonymous users

When logging out Drupal doesn't mess with the session, it gets destroyed to make sure the next request is anonymous without any session cookie and is fully cacheable. If that's not what you want then ...
4uk4's user avatar
  • 100k
2 votes

Access denied error after login

What worked for me was enabling and configuring the $cookie_domain variable in settings.php (because I have a site that can be accessed by 2 different domains). Pay attention to the comment before ...
Heitor Althmann's user avatar
2 votes

How to logout all active users?

Use Force Logout Module .. Features Settings form where you can enable and disable (Force logout). Permission so you can exclude some user roles if you want to exclude administrator for example.
Saud Alfadhli's user avatar
2 votes

How to close and start sessions?

Calling user_logout (Drupal 8) will do the trick. This essentially calls $session_manager->destroy(); as well as invoking other hooks. The user_logout function also redirected the user in Drupal 7 ...
mradcliffe's user avatar
  • 5,917
2 votes

Storing data / session for anonymous user

I wrote this module to solve this problem in a generic way. https://www.drupal.org/project/anonymoussession use this code before using the session, and you application works for both known and unknown ...
Erlend ter Maat's user avatar
2 votes

Add custom value in session

Drupal bootstrap takes care of session_start for you, so you should be able to define sessions like... $_SESSION['my_data'] = 'my value'; You can also add the following in settings.php to determine ...
Collins's user avatar
  • 2,393

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