Using Laravel 5.1 & 'file' Session Driver,
I'm trying to provide facility to user to track their sessions and invalidate them anytime they wish by keeping a record of their session_id
within the database. With Database, I mean, I maintain a table called user_sessions
which associates user_id
with their session_id
(obtained by Session::getId()
).
So, to invalidate Session, I tried the following code,
$sessionId = Session::getId();
Session::setId($sessionId);
Session::invalidate();
and it works perfectly fine, for the case where, where user does not uses Remember Me feature.
For the case where user uses Remember Me feature, this above code does not work, So, I additionally, tried setting remember_token
field to null
as specified here in this answer, but with this, all sessions of the user get destroyed including the current one.
You can simply use
If you want to get the current session Id:
I hope it helps
Sessions are meant to be short-lived. If you want something a bit more permanent you can use some sort of a long term user settings table.
Create a table
user_settings
:Add a model:
You can also associate the user with these via :
You can then get all user sessions via:
When a user logs in regularly or automatically via a remember token a
Login
event is fired.You can listen to this in your event service provider:
Note that you will have to manually persist things that need persisting instead of just putting them in the session.