The question is in the title !
I need session_id in hook_user_login.
I can get user name, user id, but no success for sessionid

 I tried  $session_id = \Drupal::service('session')->getId(); //empty

Thanks in advance for your help 

Edit: If I cannot get the id, how can I get the hash that is stored in base ?.

 

Comments

jaypan’s picture

I don't have the answer to your question, but in 17 years of coding with Drupal, that's not something I've ever needed to do. It doesn't seem like you should ever need the ID. What is it you're trying to do?

Contact me to contract me for D7 -> D10/11 migrations.

gilbertdelyon’s picture

Thank you Jaypan,
You are right, I made mistake.
In the meantime I learned how to use session variarables and it will suit my needs on a much better way.
 

gilbertdelyon’s picture

So, now I have another question about sessions
I set a varianle this way in the current session:

\Drupal::request()
->getSession()
->set("item", "value");

And I get the value this way:

\Drupal::request()
->getSession()
->get("item");

It works for the current session, but in a CRON job I would like to get the "item =>value" of every session in database.
Is there a simple Drupal way for this or should I write a query to get the "session" column values from "Sessions" table.

jaypan’s picture

Generally in Drupal you shouldn't need to use the session at all. What is it you are trying to do?

Contact me to contract me for D7 -> D10/11 migrations.

gilbertdelyon’s picture

The site is a very small community site (a few tens of registerd users - 100% private) mainly used to display images and videos of family events and vacations + some blog articles + comments.
I want the medias to be private.
I have tested "private" file system. It works, but heavy medias (example: some 2Go videos) are displayed with latency. You need to wait up to 3 or 4 minutes before you can play the video (while only 10 to 15 seconds when stored in public files). I read that it could be faster with Xsendfile Apache module, but it is not available in our shared server.
So, what to do?
I have developped a small module that add a dedicated cookie and alter the htaccess file of public folder

hook_user_login(){
//add a random value dedicated cookie

//add a "require" line in htaccess file that will check the cookie
// add a session variable that will record the cookie value

}

hook_user_logout(){
//remove the corresponfing "require" line in htaccess file
}

I know it's a kind of DIY and not at all a good practice. BUT IT WORKS very vell and suits my poorman needs.
I would be happy if someone could suggest a better idea.

And now there is an issue:
When a logged user deletes the cookies in his brower the orphan session will remain:
- in DB
- in my altered htaccess file

The orphan session will be removed after a while by drupal system in DB, but not the one in htaccess.
So, from time to time (once a day) a CRON job will compare and clean the htaccess file. AND this is why I need to read session variables in all DB sessions and, in htaccess, delete the "require" lines that are not in DB.

In the mean time I found a way (inspired by this page) , and it works! 

Please do not hesitate to suggest other ways. I am only a part time hobbyist developper.

Thanks in advance

jaypan’s picture

You know what, if it works, it works, and you've found a solution. I don't think I would choose this method myself, in case something went wrong in writing to .htaccess which would shut down your site. But if you don't have many users, and are comfortable with it, you've found a good solution.

An alternative to using session storage would be to use the tempstore.private service.

$storage = \Drupal::service('tempstore.private')->get([MODULE NAME]);
$storage->set($key, $value);
$value = $storage->get($key);

The private tempstore is essentially Drupal's replacement for sessions, preventing issues like the one you are facing with the session ID's being deleted.

Contact me to contract me for D7 -> D10/11 migrations.

gilbertdelyon’s picture

Thank you Jaypan,

You suggest tempstore.private service. I have never used this service before. I will check what I could to with it.

Session storage has the great advantage that, if the user deletes the cookies in his browser, orphan data his automatically deleted in DB after a while by Drupal system.

I have no idea if data stored  by tempstore.private  service would also be automatically deleted ? 

May be the answer is here https://drupal.stackexchange.com/a/237691 ?
 

gilbertdelyon’s picture

I need data to persist as long as session persists because it's a cookie value record.
It seems that by default the tempstorestrorage.private will be deleted after a week while session may persist much longer (30 days?).
So, I have the idea that tempstorestorage will not suit my needs. 
What do you think?

jaypan’s picture

Where are you seeing that the private tempstore is deleted after a week?

Contact me to contract me for D7 -> D10/11 migrations.

gilbertdelyon’s picture

It's written in this page. (One week by default)

By the way. Do you know if the hook_user_logout is triggered when a session will reach is lifetime and is automatically deleted by drupal system, or only when user will logout from his browser?

jaypan’s picture

Interesting, I don't think that was previously stated.

The other option you have then is the user.data service. This does not expire:

Contact me to contract me for D7 -> D10/11 migrations.