Jump to content

how to set online/offline status if the browser exit without clicking the logout butn

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
hamayun_4u2004

hamayun_4u2004

    Newbie

  • Members
  • Pip
  • 7 posts
HI.... hope all are fine... dear i am facing a problem... but em unable to get rid of it... plz tell me the solution..

the problem is..... i want to check and show the user online/offline status... for this purpose i have taken 1 feild in MY DB names is USER ON OFF STATUS thats BINARY FIELD (0/1)..

when user goto login page and put there user name and password, iuser is authentic then the field is update to (STATUS=1) .. and when user click at logout, Feild is update to (STATUS=0).

so here come my main problem.. if user become online and then user directly click at browser exit Button, then in the DB USER ON OFF STATUS field remain =1, which mean user is online, but actuly user is closed the window...

so there is any way to solve this problem..
means to signout automaticaly (if browser closed directly) and set the
USER ON OFF STATUS=0

plz tell me the solution...

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Implement last activity into your database, and set a threshold say for 3600 seconds or one hour, and set a cron job to check the database for any entries currently over the threshold (regardless if the user is there or not) you can safely log them out. This would prevent an active user from being logged out as they would be within the threshold time, it is the only safe method you can use for this.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
Scuby

Scuby

    Newbie

  • Members
  • Pip
  • 8 posts
Hi... this may not be the right solution but it's kind of simmilar...

Let's have on your index.* some code which will write into DB current time(); to the according to the current user

mysql_query("UPDATE users SET last_action='".time()."' WHERE id='".$id_of_user);

then just check it with curent time...

if((time()-300) > $db_info['last_action']){
$online = 0;
}
else{
$online = 1;
}

not the best solution but you can combine that... you don't have to log out your users and you can show to others if they are online...

#4
hamayun_4u2004

hamayun_4u2004

    Newbie

  • Members
  • Pip
  • 7 posts
dear.... em not getting this code... plz can u provide me complete code for logout, and what should i take in my DB, how to set and find user online time etc... because em new in php and having no such experties like u ppl... so plz tell me the code.... i shall be very thankfull to u...


Scuby said:

Hi... this may not be the right solution but it's kind of simmilar...

Let's have on your index.* some code which will write into DB current time(); to the according to the current user

mysql_query("UPDATE users SET last_action='".time()."' WHERE id='".$id_of_user);

then just check it with curent time...

if((time()-300) > $db_info['last_action']){
$online = 0;
}
else{
$online = 1;
}

not the best solution but you can combine that... you don't have to log out your users and you can show to others if they are online...


#5
hamayun_4u2004

hamayun_4u2004

    Newbie

  • Members
  • Pip
  • 7 posts
dear.... em not getting this cornjob and threshol????? can u explain me this ?????... and plz can u provide me complete code for logout, and what should i take in my DB, how to set and find user online time etc... because em new in php and having no such experties like u ppl... so plz tell me the code.... i shall be very thankfull to u...


Nullw0rm said:

Implement last activity into your database, and set a threshold say for 3600 seconds or one hour, and set a cron job to check the database for any entries currently over the threshold (regardless if the user is there or not) you can safely log them out. This would prevent an active user from being logged out as they would be within the threshold time, it is the only safe method you can use for this.


#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
We aren't going to give you everything, if you've given us nothing.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
asdfer

asdfer

    Newbie

  • Members
  • Pip
  • 2 posts

hamayun_4u2004 said:

HI.... hope all are fine... dear i am facing a problem... but em unable to get rid of it... plz tell me the solution..

the problem is..... i want to check and show the user online/offline status... for this purpose i have taken 1 feild in MY DB names is USER ON OFF STATUS thats BINARY FIELD (0/1)..

when user goto login page and put there user name and password, iuser is authentic then the field is update to (STATUS=1) .. and when user click at logout, Feild is update to (STATUS=0).

so here come my main problem.. if user become online and then user directly click at browser exit Button, then in the DB USER ON OFF STATUS field remain =1, which mean user is online, but actuly user is closed the window...

so there is any way to solve this problem..
means to signout automaticaly (if browser closed directly) and set the
USER ON OFF STATUS=0

plz tell me the solution...

As someone said before, you can use 'time' to see if the user is online...
You don't need a 'USER ON OFF STATUS' field in database, you need 'ACTIVITY' or 'LAST VISIT' or anything else. But let's use 'ACTIVITY'.
Now, you have to place this code on every page the user can visit:
mysql_query("UPDATE users SET activity='".time()."' WHERE id='".$userid);
Now, you choose a time limit, after a user will be considered offline. Let's say 1 minute (60 seconds). And you will make the online check every time you need:
echo '<div id="blah">';

$result = mysql_query("SELECT activity FROM users WHERE id = '".$userid);

list($useractivity) = mysql_fetch_row($result);

if($useractivity>time()-60) echo 'offline';

else echo 'online';

echo '</div>';

?>

You can use a function to call it when you want
function checkUserStatus($userid) {

$result = mysql_query("SELECT activity FROM users WHERE id = '".$userid);

list($useractivity) = mysql_fetch_row($result);

if($useractivity>time()-60) echo 'offline';

else echo 'online';

}


echo '<div id="blah">'.$username.' is '.checkUserStatus($userid).'</div>';