Jump to content

[SOLVED] logged in users - only one at a time.


wrathican

Recommended Posts

heya.

 

Ive been racking my brains as to how to go about this.

 

i come up with one thing but that got dimissed right away.

 

Is there a way in which i can prevent a user logging in if someone else is already logged in?

 

Well, i thought it would be easy and that the best bet would be to use MySQL to store a value next to the user logged in.

 

eg:

user logs in -> value is set in table

another user tries to log in -> check if anyone is already logged in

if not -> log user in

if so -> tell them to wait or something

 

But it came apparent that, if a user log's in, and then closes to the browser. it would leave that user logged in according to the database.

 

I see forums can determine who's online, but thats based on active users over the past 5 mins. not sure how it accomplishes it but help is appreciated..

 

Thanks

 

Wrathican

I'd use a timestamp in the DB as well. While a user is navigating a site, every page loaded should refresh the timestamp.

 

That way, if they close the window, the timestamp won't be being refreshed, and then another user tries to log in, you check the timestamp as well as the status - if the timestamp is more that 10 mins old (or whatever) change the user to offline, and allow the new user to log in.

 

Make sense?

I would store the users session ID in the DB and update the timestamp in the DB everytime the user who's session is registered loads a page. I would then do a second function so when a user loads a page it checks which sessions in the DB are over 10 minutes old and then delete the sessions that fit this criteria from the db. Something like this might point you in the right direction.

 

$sqldelete = "DELETE FROM your_table WHERE (TIME_TO_SEC(now()) - TIME_TO_SEC(sessiontime)) >=600";

O, FYI. If a user sits on one page for more than the time period (say 5 mins), you may find it causes problems - allowing another user to log in, when the 1st user didn't actually want to log out.

 

You could get round this using AJAX though I suppose - Having an AJAX element on each page that will say "hello" to the server every four minutes, would keep the timestamp current, even if the user needs to sit on a page for a while.

 

That'll also cause the problem that a user could be annoying and stay indefinitely logged in, but you could always get round that using a session as well.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.