Jump to content

[SOLVED] Active users online


Trium918

Recommended Posts

best way is to not use the default session path. make your own, then count how many session files there are in that directory. google will reveal the exact code of it.

 

it's better than tryin to do any elaborate sql work to 'try' to figure out who's still logged in....

Link to comment
Share on other sites

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";
?>

Link to comment
Share on other sites

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();
}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.';

Link to comment
Share on other sites

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']." ";
} 

?>

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.