Can someone explain session table laravel
Asked Answered
Z

1

1

Ok so I want to check if my users are online or not, I think a good way of doing this is using the sessions table that laravel provides. I'm a newbie though so can anybody first of all explain to me what this data means.

CREATE TABLE sessions ( id VARCHAR(40) NOT NULL, last_activity INT(10) NOT NULL, data TEXT NOT NULL, PRIMARY KEY (id) );

last_activity is a unix timestamp, that one is clear to me.

My id = lGBzJ0dIWiebrwjFlkQAE19NHuNHvsPRVS7e4CRO

This is equal to the cookie value but that doesn't say all that much to me.

and my data = a:3:{s:5:":new:";a:0:{}s:5:":old:";a:0:{}s:10:"csrf_token";s:40:"XOZbMp8mrVUbu2Lk4c1cEdCoJUQIL3tSeHaztH1v";}

What do these 2 mean and how can I use them to derive a user id from them in laravel?

Zelmazelten answered 11/6, 2013 at 18:35 Comment(0)
P
1

Laravel does not store a reference to the user directly on the sessions table. It does not matter to the session system who owns the session, and there may be sessions with no user attached to them (i.e., before someone has logged in).

The data column is PHP serialized so if you really want to read that data you can.

Laravel breaks session data in to 3 categories: persistent, old flash data, new flash data. This is why you will find two arrays ":new:" and ":old:", empty in your pasted code.


Why don't you have a "last seen" column on your users table, touch it each time a user loads a page, and if it's < x minutes since they were last seen then they can be considered "online"?

Phonate answered 12/6, 2013 at 8:16 Comment(2)
Thanks this is a very usefull answer, atleast I know what I'm working with know. I've seen that laravel does have last_activity in the sessions table and this seems to work perfectly for my cause of implementing it into my users table. I can only backtrace It to laravel/sessions/database.php though. Do you happen to know where I can find the code that laravel uses to check for last activity?Zelmazelten
Laravel does not use the session's last_activity outside of the session driver. It is used to sweep (clean up) old sessions from the session table, nothing else.Phonate

© 2022 - 2024 — McMap. All rights reserved.