views:

45

answers:

2

I want to prevent multiple log in with same log in credentials simultaneously. So I made a column login_status and set it to 1 when some one logging in and change to 0 when logging out besides I set session after successful logged in. If user won't click on log out(in case of user close tab or because of some network problem) it doesn't update database and then one can't use that log in credentials again. So I use a ajax call to set current time stamp in database with related log in credentials and it is updated in each 2 minutes if user not navigate from that page. Then if some one attempts to log in with same log in credentials, it will check these time stamp if column login_status is 1, then if the time stamp is older than 3 minutes it allows the log in.Then it solving that problem. But the new problem is if user closes the tab or browser window and after 3 minutes one can log in with same log in credentials from somewhere and if the previous user open that page automatically it will log in as session is already set. How can I prevent this.

+1  A: 

Your approach is way to complicated and vulnerable for race conditions. I suggest you store your sessions in the database. This way you can always check if a user is already logged in and overwrite his/her previous session if he logs in twice. This way only one session will be valid.

Have a look at http://php.net/manual/en/function.session-set-save-handler.php

halfdan
+1  A: 

I would not suggest saving sessions to database, unless absolutely necessary. You will put more strain on the database than needed.

What I would suggest is to create a new table like users_logged_in and insert the login ID and timestamp of the user who's logging into the application but not before checking the table first to see if the user exists.

You can then delete any expired user from the table at every page request or a N page request, where N is a number of page requests.

Syed