Question about tracking logins/logouts

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
OldRod
Posts: 1320
Joined: Sun Sep 20, 2009 4:26 pm

Question about tracking logins/logouts

Post by OldRod »

I have a field in my db called "loggedin" which is a simple 1 or 0 depending on whether the player is logged in or not. The login code sets it to 1 and it remains at that value until the player logs out.

I also have a logout page which then sets this to 0 and destroys the session, but what happens when the player closes the browser window without logging out?

How do I execute the logout code and set this field to 0 in that instance?
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Question about tracking logins/logouts

Post by hallsofvallhalla »

many of these are set on timers. Like crons. They run every 15 minutes and check last query VS current time. If current time - last query = to long it sets the value to 0. I know this may not be what you are looking for but its the best I got right now but I will look into this.
Falken
Posts: 438
Joined: Fri May 08, 2009 8:03 pm

Re: Question about tracking logins/logouts

Post by Falken »

To keep track if a player is online or offline there is often no need for crons.

Simpy save the time when the player last did something in the database. Then just check if that was in the last X minutes. (X = how long they should be allowed to be idle without being logged out).
--- Game Database ---
For all your game information needs!
Visit us at: http://www.gamedatabase.eu today!
User avatar
OldRod
Posts: 1320
Joined: Sun Sep 20, 2009 4:26 pm

Re: Question about tracking logins/logouts

Post by OldRod »

But how would you check that?

If they aren't using their browser anymore, there are no pages being loaded for the check to be made. It's like this has to be run on the server somehow?
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Question about tracking logins/logouts

Post by hallsofvallhalla »

that is where the cron comes in...
User avatar
OldRod
Posts: 1320
Joined: Sun Sep 20, 2009 4:26 pm

Re: Question about tracking logins/logouts

Post by OldRod »

Guess I need to review that tutorial on crons then :)
Falken
Posts: 438
Joined: Fri May 08, 2009 8:03 pm

Re: Question about tracking logins/logouts

Post by Falken »

OldRod wrote:But how would you check that?

If they aren't using their browser anymore, there are no pages being loaded for the check to be made. It's like this has to be run on the server somehow?
You don't check, unless someone wants to know if they are online or offline.
In database field 'last-action' you save for example '2009-12-31 12:30'. And then if someone wants to see if he is offline or online you just check how long since his last action, and it was more than lets say 15minutes then it says offline otherwise online.
--- Game Database ---
For all your game information needs!
Visit us at: http://www.gamedatabase.eu today!
User avatar
OldRod
Posts: 1320
Joined: Sun Sep 20, 2009 4:26 pm

Re: Question about tracking logins/logouts

Post by OldRod »

What I'm trying to do is keep someone from logging in twice from two different browsers at the same time with the same username.

I had planned to have a "logged in" field set to 1 if they were logged in, and and 0 if they logged out, then check that field on subsequent login attempts. But as I was testing it, I ran into the problem of not logging out on one browser, so I got the "already logged in" message when I really wasn't.

Is there a different or better way to do this?
User avatar
Chris
Posts: 1582
Joined: Wed Sep 30, 2009 7:22 pm

Re: Question about tracking logins/logouts

Post by Chris »

Erm.. put in the timestamp they logged in. Using the time() function. This will give you the ammount of seconds that have passed since January 1st 1970. :O. Okay, not important though. Then check to see if 60*the ammount of minutes you let them be inactive for before declaring the are offline + the last active field in the database is greater than time().

In the hope this wont be asked again I'll post it now.
Updateing the table with the user information

Code: Select all

<?php
mysql_query("UPDATE `usershit` SET `last_active` = '". time() ."' WHERE `id` = '$theUsersId'");
?>
Getting an array of the users online in the last five minutes

Code: Select all

<?php
$userQuery = mysql_query("SELECT * FROM `usershit` WHERE `last_active`+300 > ". time() .""); // 5 mintues I think?
while( $userArray = mysql_fetch_assoc($userQuery) )
{
    echo $userArray['username']."<br />\n";
}
?>
Fighting for peace is declaring war on war. If you want peace be peaceful.
Post Reply

Return to “Advanced Help and Support”