Page 1 of 1
Question about tracking logins/logouts
Posted: Fri Jan 01, 2010 10:14 pm
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?
Re: Question about tracking logins/logouts
Posted: Fri Jan 01, 2010 10:24 pm
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.
Re: Question about tracking logins/logouts
Posted: Fri Jan 01, 2010 11:18 pm
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).
Re: Question about tracking logins/logouts
Posted: Fri Jan 01, 2010 11:27 pm
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?
Re: Question about tracking logins/logouts
Posted: Sat Jan 02, 2010 12:26 am
by hallsofvallhalla
that is where the cron comes in...
Re: Question about tracking logins/logouts
Posted: Sat Jan 02, 2010 12:45 am
by OldRod
Guess I need to review that tutorial on crons then

Re: Question about tracking logins/logouts
Posted: Sat Jan 02, 2010 11:43 am
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.
Re: Question about tracking logins/logouts
Posted: Sat Jan 02, 2010 1:54 pm
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?
Re: Question about tracking logins/logouts
Posted: Mon Jan 04, 2010 10:37 pm
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";
}
?>