Search code examples
phpsymfonysessionsymfony-3.3

Single session implmentation in symfony 3.3


I am currently searching for a way to implement single session in symfony 3.3, what I want is that if I log in from one browser then log in from another browser on the same user, I want to be logged out from the first session.

A non simple way would be to store the latest session id in the user entity and then query that on every request, if the session id is not the same and older then the user gets redirected to the logout route.

I was wondering if anyone knows a simpler way to implement this which might not be in the symfony documentation.

Thanks.


Solution

  • I solved this by using redis cache instead, I saved the user id as a key and the session id as the data inside the key. Then on every request I searched in redis if there was a key for the current user. If not then I created a key for that user with the session id as the data. If yes then I checked if the session id inside the key was the same as the current session id. If it was the same then I just continued normally, if not I deleted the old session from the session handler and put the new session inside the key.

    I also use redis as session handler so I just called destroy on the old session id.