tags:

views:

143

answers:

6

Hi,

I am developing the user management portion of a website that will host a webcast. The goal is to prrevent the same user nam (email address) from being used concurrently. That is, we don't want two individuals using one login to view the event.

I've already setup a table that holds the user registration data with regID as primary key. My thought is to create a login history table with username as primary key, foreign key to user name in registration table. The login history table would simply timestamp when the user logs into the site. However, this won't accomplsih my goal of preventing more than one individual from using the same login name.

Instead, would it be better to have a login status field either in the login history or user table that is set to 1 for logged in and 0 for logged out? It would need a stored procedure to update the value at login and at logout, and would need to be validated when a user logs in such that if login status = 1, user already logged in and cannot login a second time. Is this a feasible approach?

Please share other methods you've used to prevent the same login credential from being shared amongst multiple individuals.

Thanks, Sid

A: 

The problem here is detecting the user is logged in (i.e. whether he didn't logout).

One possible way is to register in the database the time of his last activity and the time of his explicit logout. You could then deny a login if it this was attempted less than say 5 minutes ago relatively to his latest activity and if he didn't login in between.

You could force "activity" by having the website pages periodically poll the server with Javascript.

Artefacto
Would it be feasible to create a function that checks the loginhistory table to see if the user has a login transaction in the last 5 minutes wihtout a corresponding logout transaction? Thanks:)
SidC
@SidC You could but then someone else could login after those 5 minutes.
Artefacto
A: 

It's easy to determine when someone logs in. It's much harder to determine when someone logs out. If you have a mechanism of killing the webcast streaming to a particular user quickly, you might want to have something which pops up asking the user if they want to kill their other session if you think there might be one active.

Yuliy
A: 

You might want to consider making a global variable in php to store a hash array with login status. This has the benefit that if the application has to be restarted for some reason, the user isn't stuck in the wrong state in the database.

You can store a mapping from user ID to IP or session cookie and redirect requests that come with different information to the login page. If the user logs in, the other session would be invalidated and further requests in the last session forward to the login page.

amccausl
A: 

How are you doing user sessions on the server? If you store them in the db, you could query the active sessions anytime someone attempts to log in and see if they're already in there. Of course you'd probably also have to check some kind of timestamp since you're not guaranteed that sessions will disappear at session.gc_maxlifetime.

beamrider9
+1  A: 

Without rolling your own session handler, you could do a little parallel tracking. When a user logs in, you can store the user's session ID and login time in the database (maybe inside the user information table). The login script could then check for the existence if this sessionID and allow/deny login based on the presence of the session ID. If the ID's null/blank, then the user logs in. If there's a session ID present, and it's more than X minutes old, allow the login. Otherwise deny them.

Of course, you'd probably want to roll your own session cleanup handler at that point, so that when stale session files get deleted, you can remove the associated IDs from the database at the same time.

Marc B
+2  A: 

If it is OK to logout an already logged in user if someone else logs in with the same creditentials then you could... when a user logs in generate a random ID in your DB for that user and the same in a cookie session. The two must match to authenticate.

w3d
Replace "cookie" with "session", and this will work well. Just make sure to check the session value against the database on each request, and to always reset the value when the user logs in again. Two users trying to view the page will continually log each other out.
Charles
Yes, good call, updated post to read "session".
w3d
I like your approach. The only caveat would be that the second user, when attempting to use a login already in use, receives a message stating user ID is already logged in, or something to that effect. We want to ensure that everyone viewing the webcast has their own login credentials. Thanks:)
SidC
However, if you are going to start _preventing_ the 2nd user from logging in then you want to avoid preventing a _valid user_ from logging in again. eg. when a user accidentally leaves themselves logged in at work and then is unable to log in at home. You then need to start timing out users after a period of inactivity, as mentioned below.
w3d