Kill Duplicate Sessions On Server In Laravel 5.2

What is it about ?

The database session driver now includes user_id and ip_address so you can easily clear all sessions for a given user.

What's the Problem

I checked this Article while reading what's new in Laravel 5.2

Is there any blog that clearly explains on how to logout the duplicate sessions created using multiple IP address or through the same IP address using multiple browsers ?



As far as I see there is no difficulty with that.

Sample data in this table looks like below:

Session table data

So you have here user_id, browser and last activity timestamp. So if you want you can now add to cron for example running the following query:

select user_id, count(*) AS `total` FROM sessions GROUP by user_id HAVING count(*) > 1

This will give you users with multiple sessions and you'll be able to decide what to do with them. You can for example remove all sessions for those users or leave only the last one and remove all the others. It's up to you of course.

In the moment when you remove the record from database user will need to login again so for example above if I removed my session for Firefox, I need to login again in Firefox to be logged on my account.


Be aware that by default there is no sessions table (because many users won't use database driver for sessions). To create this table you need to run:

 php artisan session:table

The above command will create sessions migration

and then you need to run

 php artisan migrate

to apply this migration into database