Jump to content

[SOLVED] Active users online


Trium918

Recommended Posts

http://www.phpfreaks.com/forums/index.php/topic,151986.0.html

Have a piece of code on each page that updates a field in the users table with the current datetime, then in your "Currently Online" box, select all users from the table that have accessed a page within the last n minutes.

 

I already have a field in the members table called last_visit. I was

thinking maybe I can just update last_visit, so my query would look

something like the one below, correct?

 

<?php
$timeoutSeconds = 600;
$timeout = time() - $timeoutSeconds;

$query ="SELECT * FROM user_table WHERE last_visit < $timeout";
?>

Here is the query that I implemented and it is

not working. There are no errors, but I am getting

an echo "No results";

 

I am trying to determine all active users online.

 

<?php
$timeoutSeconds = 600;
$num_online = 0;

$currentTime = time();
$timeout = $currentTime - timeoutSeconds;

$online_query = "SELECT * FROM members_info WHERE last_visit < $timeout";

if($online_result=mysql_query($online_query)){
if (mysql_num_rows($online_result)) {
  $num_online = mysql_num_rows($online_result);
  if($num_online == 1) {
echo "$num_online";
  }else {
    echo "$num_online";
   } 
}else {
      echo "No results found";
  }
} else {
    echo "Query failed<br />$online_query<br />" . mysql_error();
}

?>

It should be making a error as

 

$timeout = $currentTime - timeoutSeconds;

 

Should be

 

$timeout = $currentTime - $timeoutSeconds;

 

Other then that the code looks ok, I have made a whos online script before and what people where saying above about sessions etc is not needed, and the methods you are using are fine.

I would just update a column everytime a page loads or something like that with a timestamp.... Then simply do something like:

 

$ctime = time() - 600; //10 minutes in the past
$q = mysql_query("SELECT COUNT(user_id_or_some_other_column) FROM users_or_what_ever_its_named WHERE last_active >= {$ctime}");
$r = mysql_fetch_row($q);
echo 'There are ' . $r[0] . ' users online.';

I created to users accounts.

User1 and User2

If I log in as User1 everthing is fine, but

when I log User2 in then User1 online status

is deleted from the members_online table.

 

There should be a count of two users online. What

is causing the problem of one User account being deleted?

<?php
// connect to database 
// retrieve # of users online

// I am having problems with this section
$timeoutSeconds = 60;
$timeout = time() - timeoutSeconds;

$query = "DELETE FROM members_online WHERE timestamp < $timeout";
mysql_query($query);
///////////////////////////////////////////////////////////

$query  = "SELECT username FROM members_online";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
    echo $row['username']." ";
} 

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.