问题
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?
回答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"?
来源:https://stackoverflow.com/questions/17051304/can-someone-explain-session-table-laravel