Hello everyone. Ok, I'm creating this blog/comments website and I've set up an active/not active user system. So for example when you login, your set to active and obviously you log out and you're no longer active but I've set up this piece of code for when you make no move for 15 minutes (changeable), it sets you to non-active until you make a move.
But what I am trying to do is record how long the user was signed in for. So I add a row to a database when a user logs in (activates the active user function) and when the user logs out (it updates and records how long the user was logged in for e.g. 30 mins). The problem is I want that to apply for when the user is unactive and when the user becomes active, another row is added so the main problem is, the user could stay logged in overnight and when he logs out the next day he has been signed in for 23 hours for example.
Also, when you login, you are given a specific user variable like a login-id which stays with you until you log out.
Here is my 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 is that inserts two rows when you login, one proper one with the $session->id and one without it but still stores the username but anytime I logout and back in after this, only one row is correct. Help on that?
Here is the logout (remove active user)
function removeActiveUser($username){
global $session;
$this->removeUsermap($username);
}
Here is $this->removeUsermap
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);
}
Ok and finally here is (non active for 15 mins 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);
}
}
One problem with this is that removeInactiveUsers doesn't log you out so you still have the same userid (login id) so another row isnt added when you become active again. Also removeInactiveUsers doesn't active $this->removeUsermap.
So the question overall is how can I fix this so when you are inactive, removeUsermap activates and when you become active again another row is added. Also for the first time you become active, it adds two rows?
All help appreciated, thanks 🙂