The thing I'm trying to do the now is check how long you are signed in for and how long you signed in for based on the above system. When you login, you are given a login key. What I tried to do was when you were added to the active user system you created a row in my usermap table with when you logged in and this login key then when you logged out, this row would update with when you logged out and how long you were logged in for e.g. 20 minutes. Problems occur when you stay logged in for example overnight and you're not active so when you sign out the next day, my row says you have been logged in for 24 hours.
Now, what I'm trying to do is when you are removed from the active users table for being inactive, update my usermap row and then when you were active again, add a new row until your either inactive or log out. Now the problem arises as these rows use your login id to distinguish you from previous logins and other users but when you are inactive you still have the same login id unless you log out so I can't update my rows. Here are the following pieces of code:
function addActiveUser($username, $time){
global $session;
$zone = 'GMT';
$currentdate = $today = date("l, F jS Y, H:i:s ") . $zone; //Today
$userip = $_SERVER['REMOTE_ADDR'];
$r = "SELECT * from ".TB_USERMAP." where login_id = '$session->userid' and username = '$username'";
$result = mysql_query($r, $this->connection);
if(mysql_num_rows($result) == 0){
//Only insert this once per login
$t = "INSERT INTO ".TB_USERMAP." (login_id, username, date_in, timestamp_in, ip) VALUES('$session->userid', '$username', '$currentdate', '$time', '$userip')";
mysql_query($t, $this->connection);
}
}
Also another problem arises here, if it's the first time you ever login, two rows are added. Any other login, only one row is added. What's wrong there?
function removeActiveUser($username){
global $session;
$this->removeUsermap($username);
}
function removeUsermap($username){
global $session;
$time_logged_out = time();
$zone = 'GMT';
$currentdate = $today = date("l, F jS Y, H:i:s ") . $zone; //Today
$p = "SELECT * from ".TB_USERMAP." where login_id = '$session->userid'";
$result = mysql_query($p, $this->connection);
$result2 = mysql_fetch_array($result);
$time_logged_in = $result2['timestamp_in'];
$elapsedTime = ($time_logged_out - $time_logged_in);
$totaltime = date('H \h\o\u\r\s, i \m\i\n\s \a\n\d s \s\e\c\o\n\d\s', $elapsedTime);
$t = "UPDATE ".TB_USERMAP." SET date_out = '$currentdate' WHERE login_id = '$session->userid'";
mysql_query($t, $this->connection);
$u = "UPDATE ".TB_USERMAP." SET timestamp_out = '$time_logged_out' WHERE login_id = '$session->userid'";
mysql_query($u, $this->connection);
$v = "UPDATE ".TB_USERMAP." SET time_logged_in = '$totaltime' WHERE login_id = '$session->userid'";
mysql_query($v, $this->connection);
}
Now this all works fine. Here is my removeInactive users code.
function removeInactiveUsers(){
global $session;
$timeout = time()-USER_TIMEOUT;
$r = "SELECT * FROM ".TB_ACTIVEUSERS." WHERE timestamp < $timeout";
$result = mysql_query($r, $this->connection);
$results = mysql_fetch_array($result);
if($results['username'] = $session->username){
$this->removeUsermap($session->username);
}
}
That doesn't work but even if it did, how would a new row become added when the user becomes active again since $session->userid hasn't changed. What I want is a personal id which changes each time you become active and is unique to each person and each activity.
Feel free to ask any questions if you don't understand.
Sorry for the long winded question but this has been bugging me for a while and thanks for your time.
All help will be appreciated.

New Topic/Question
Reply



MultiQuote





|