20
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 ...
15
votes
Accepted
No one can login and access denied message
The problem was ip_geoloc module. It was messing up $_SESSION variable, a solution is given on the issue queue (for now!) and the module author agrees with it. It's highly probable that it is ...
Community wiki
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 = '...
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 ...
13
votes
Accepted
How to get an anonymous user session id?
You're looking for PHP's session_id():
session_id() is used to get or set the session id for the current session.
7
votes
Accepted
What is the best way to tell anonymous users apart?
Session variables seem to be the way to go
Don't forget that session variables rely on cookies
Have I missed any options?
The traditional solution would be to use the ip address as a fallback if ...
7
votes
What is the right way to force a session start?
Okay, found the answer from this change record: https://www.drupal.org/node/2228871
The session can be force-started like this:
// Force session start if we don't already have a session.
$session = \...
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 ...
7
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 ...
6
votes
Accepted
How can I programatically log another user out in D7?
drupal_session_destroy_uid() sounds like a safe bet:
Ends a specific user's session(s).
The core implementation clears the database, as you'd expect, but any module that overrides session.inc, and ...
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:
$...
6
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)) {
...
6
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 ...
5
votes
How to keep the size of the database reduced?
Don't dump session, cache_*, watchdog table content (make sure to keep the structure).
For watchdog there's syslog replacement.
If you don't need the access logging in Drupal disable that module and ...
5
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 ...
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 ...
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 ...
4
votes
Accepted
Use Redis for Drupal sessions
a quick google search: http://lmgtfy.com/?q=drupal+redis+sessions
and the 2nd result talks about redis session handling:
https://www.drupal.org/node/2250195
from that issue thread i recommend ...
4
votes
How do I save session data for the anonymous users?
It because drupal save session with key, this mean you should do something like
$_SESSION['yourkey']['xxx'] = serialize($data);
You can try this.
function lists_session($key, $value = NULL) {
...
4
votes
Accepted
How to set different logout times for roles?
We can achieve that using the Automated Logout module. combination of the Javascript Timer module.
Using the Automated Logout module we can set different timeouts for user roles and using Javascript ...
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 ...
4
votes
How to get an anonymous user session id?
Drupal stopped tracking anonymous sessions a few versions ago. If you want to trick Drupal into doing it again you need to make a module with a custom hook_init() implementation.
/**
* Implements ...
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 ...
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');
...
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 ...
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.
3
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.
...
3
votes
How do I store the values submitted in a form in the session?
Here is an article on how to use the appropriate service to store data in the session: http://atendesigngroup.com/blog/storing-session-data-drupal-8
There are two methods described in the article, ...
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, ...
3
votes
Accepted
How to keep the size of the database reduced?
Try the modules
Optimize Database, and
Database maintenance
The differences are also explained in the this fixed issue Compare with DB Maintenance in the Optimize Database issue tracker.
The first ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
sessions × 2327 × 68
8 × 55
users × 53
cookies × 33
6 × 13
services × 12
forms × 10
theming × 7
redirection × 7
database × 6
ajax × 6
caching × 6
views × 5
nodes × 5
commerce × 5
javascript × 4
authentication-authorization × 4
bootstrapping × 4
hooks × 3
webforms × 3
rules × 3
i18n-l10n × 3
testing × 3
ssl × 3