Jump to content

Way to see if someone is online.


smithmr8

Recommended Posts

it doesn't update the user if he has gone offline or AFK - that is the whole point.  his last action time will be MORE than 5 minutes ago if he has done either of these things.  therefore selecting the list of users with last action times that are more recent than 5 minutes ago ensures that your list is accurate (to 5 minutes).

Link to comment
Share on other sites

there's no harm in leaving the entry for each user, since it will simply be re-inserted the next time the user visits the website anyhow.  furthermore, not deleting those rows gives you one more valuable piece of info: the last time they WERE active on the website.

 

all you need to do is update their timestamp if it's older than whatever interval you choose (here, 5 minutes).

Link to comment
Share on other sites

1 hour example........

<?php
$now = time();
$lastLogin = strtotime($row['lastLogin']);
$diff = $now - $lastLogin;
$now = date('YmdHis',$now)

if($diff > 3600) { // 3600 seconds is 1 hour
$query = 'UPDATE members SET logins = logins + 1, lastLogin = '.$now.' WHERE memberID = '.$SEC_ID;
mysql_query($query);
}
?>

Link to comment
Share on other sites

I'm trying to get it to insert the base values into the Table if it is their first time logging in, but there appears to be something wrong with my mysql query, but I dont know what it is. If I comment out the query, the page loads. If I dont, it just shows a blank page.

 

<?php
$user = $_SESSION['myusername'];
$info="SELECT * FROM users WHERE username='$user'";
$result_info = mysql_query($info);
$x=mysql_fetch_array($result_info);
if($x['logins'] == 0) {
$ip = $REMOTE_ADDR; 
$timestamp = date("d/m H:i:sA");
mysql_query= "INSERT INTO `online_users` (`userid`, `ip`, `last_active`) VALUES ('$x[iD]', '$ip', '$timestamp')" or die(mysql_error()); 
}
?>

Link to comment
Share on other sites

Ok, I've got the 'Last Active' working now. Im just having a bit of difficulty getting this part to work.

Its showing the 'Online Now' image for everyone, even though they were last active more than 5 minutes ago.

I dont know whether its the way I've stored the timestamp ?

<?php

$lastLogin = $view2['last_active'];
$now = date("d/m H:i:sA");
$diff = $now - $lastLogin;

if($diff > 300) { // 3600 seconds is 1 hour
echo "<img src=offline.png><br>";
} elseif ($diff < 300) {
echo "<img src=online.png><br>";
}
?>

Link to comment
Share on other sites

If the two dates and times are timestamps, then you just subtract them and that'll give you the difference in number of seconds.

 

If they're in another format, such as 'yyyy-mm-dd h:i:s', then look into one of MySQL's date functions, specifically DATEDIFF():

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

Link to comment
Share on other sites

Timestamps are just numbers of seconds, neither is in any format until you format them with a MySQL function or a call to PHP's date().  So perform your calculation before you convert them.

 

Or convert them back to timestamps with PHP's strtotime().

Link to comment
Share on other sites

Oh. Im having trouble doing any of that at the moment. I have tried using something like the code which was suggested before, but it didn't work. Everything I try now, just doesn't seem to work either.

 

I have a timestamp which was stored in a Table. That is supposed to be used, along with the current time to determine the time difference between the two, preferably in seconds.. which I can then use to determine whether or not the user is still online.

 

$view2['last_action'] = Stored Time Stamp, stored in the format.. ("d/m H:i:sA")

 

I could really use a hand with this.

Thanks.

 

Link to comment
Share on other sites

You should read the MySQL documentation on TIMESTAMP columns to figure out exactly how they work, but from what I recollect, the first timestamp column of a table will auto-update to the current timestamp any time you insert or update a record.

 

Otherwise you want to change your code from finding the TIMESTAMP in PHP and just use MySQL's NOW() function:

// I've dropped your $timestamp variable
mysql_query= "INSERT INTO `online_users` (`userid`, `ip`, `last_active`) VALUES ('$x[iD]', '$ip', NOW())" or die(mysql_error()); 

 

Then if you want all the users online in the past 5 minutes:

  SELECT * FROM `online_users` WHERE NOW() - `last_active`>=300

 

Do you know how to use JOINs to connect the usernames to their IDs in the online_users table?

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.