Jump to content

[SOLVED] Offline/Online status help?


djfox

Recommended Posts

Its a common misconception. You shouldn't be storing an online/offline status, because, as you've noticed, you run into issues if someone closes the browser.

 

What you should be doing is storing a 'last active' timestamp. This will, of course, need to be updated on each page. When working out who is online, you'll select only those users who were last active in, say, the last 300 seconds (5 minutes).

Link to comment
Share on other sites

put the time stamp into your mysql table, when they log in update the time. You can then use that time to check and see if he is still logged in. Whenever he visits a new page update the time in the db with the new time. If that time stamp has not been updated for 5min safe to say he's offline usually and if not when he visits a new page it will update that time stamp and put him back online.

Link to comment
Share on other sites

I have it where when the user logs in, their status switches to online, and when they click the logout link, their status changes to logout. But how does this get changed when they just close the browser?

If you're asking HOW it gets changed when the person closes their browser, the answer is sessions.

Link to comment
Share on other sites

The recommended way to handle this is that when the person visits a page, their time slot in the database gets updated? Ok, I have one other question in regards to that.

 

This the method of recording time/date in the database I use for my site:

strftime("%B\ %e\,\ %Y %H:%M:%S", time())

 

How do I check the time to see if they get counted as online?

Link to comment
Share on other sites

I have it where when the user logs in, their status switches to online, and when they click the logout link, their status changes to logout. But how does this get changed when they just close the browser?

If you're asking HOW it gets changed when the person closes their browser, the answer is sessions.

 

That makes no sense, honestly. A PHP script cannot determine when a session has ended, unless it checks (but then that means the person's online and defeats the purpose)

 

The recommended way to handle this is that when the person visits a page, their time slot in the database gets updated? Ok, I have one other question in regards to that.

 

This the method of recording time/date in the database I use for my site:

strftime("%B\ %e\,\ %Y %H:%M:%S", time())

 

How do I check the time to see if they get counted as online?

 

Here's how I would do it, personally. Timestamps, use the time() alone and store that as an int string in the database.

 

So.. I visit a page, the following code runs:

 

<?php
@mysql_query("UPDATE `users` SET `last_active` = ".time()." WHERE `username` = $user LIMIT 1") or die(mysql_error());
?>

 

This just basically gets the current timestamp and sets it in the database... Then checking all users to see who is online...

 

<?php
$inactivity = 300;//300 seconds = 5 minutes

$inactive_time = time() - $inactivity;//all users active since this time ago are considered still active

$query = "SELECT username FROM `users` WHERE `last_active` >= $inactive_time ORDER BY `last_active` DESC";

while($row = mysql_fetch_array(mysql_query($query) or die(mysql_error()))){
echo $row['username']."<br>";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.