Jump to content

Checking Active Members Online


Exoon

Recommended Posts

Well, there are a few ways. If you want just the number of users viewing your site, you can count the number of files in the directory that your server uses to store $_SESSION files. Of course, that only works if you use $_SESSION variables instead of $_COOKIE variables.

 

Another way would be to add a field into your database. (Let's call it `active` and give it the ENUM('0', '1') field type -- '0' for when they are not logged in and '1' for when they are logged in. When a user successfully logs in, update that user's `active` field to '1'. And when they logout, update that user's `active` field to '0'. And then (to display a list of online users), just perform an SQL-Query that pulls all users from your user-data table with a '1' in their `active` field.

create table user (
   ..
   last_click_at datetime,

 

Update the user table to indicate his last activity then using a query like:

 

SELECT count(*) online_users FROM user WHERE last_click_at BETWEEN now() - 300 AND now()

 

Will consider all users who clicked between now and 5 minutes ago as online

 

@Goldeneye what do you do when they don't logout? They remain logged in forever?

create table user (
   ..
   last_click_at datetime,

 

Update the user table to indicate his last activity then using a query like:

 

SELECT count(*) online_users FROM user WHERE last_click_at BETWEEN now() - 300 AND now()

 

Will consider all users who clicked between now and 5 minutes ago as online

 

@Goldeneye what do you do when they don't logout? They remain logged in forever?

 

That is  a good point. With my system, you'd want to implement a last-active field as well. Which would defeat the purpose of the `active` field I talked about.

create table user (
   ..
   last_click_at datetime,

 

Update the user table to indicate his last activity then using a query like:

 

SELECT count(*) online_users FROM user WHERE last_click_at BETWEEN now() - 300 AND now()

 

Will consider all users who clicked between now and 5 minutes ago as online

 

@Goldeneye what do you do when they don't logout? They remain logged in forever?

 

 

Hello,

 

With the query. Do i need to update it for the specific username. Also how would i display that on my actual page what would i need to echo?

Yes you need to update the user record on each request like:

 

if ($auth->hasIdent()) {// performs challenge-key check and set's new challenge key
    $user->setLastClickAt(time());// performs timestamp to date transformation using strftime()
    $userTable->save($user);
}

 

Finding all users between the interval can go like:

 

$users = $userTable->findUsersByLastClickBetween(5);// interval in minutes
if (!empty($users)) {
    $view->activeUsers = $users;//$users are colored based on $user->isAdministrator(), $user->isModerator(), ..
}

 

Don't forget to index last_click_at

My code is just an example pseudo-code if you like. I tend to use OO as much as possible in plain/raw PHP this would be:

 

To update the user record:

if (isset($_SESSION['logged_in'])) { // only update the record if the user is actually logged in
    $query = 'UPDATE users SET last_click_at = now() WHERE id = ' . mysql_real_escape_string($_SESSION['user_id']);
    mysql_query($query, $database);
}

 

To find all logged in users:

$query = 'SELECT * FROM users WHERE last_click_at BETWEEN now() - 300 AND now()';
$result = mysql_query($query, $database);

$users = array();
if (false !== $result) {
    $resultCount = mysql_num_rows($result);
    if (false !== $resultCount) {
        $users = array_map('mysql_fetch_assoc', array_fill(0, $resultCount, $result));
    }
}

print_r($users);

 

An other possible way is by storing session data in the database. Then to find all logged in users it's as simple as:

 

SELECT * FROM session WHERE modified + lifetime > now()

 

Here's an example: http://framework.zend.com/manual/en/zend.session.savehandler.dbtable.html

 

This also eliminates the need to update the record on each request as your session save_handler will do this for you (altough you still need to provide the implementation).

I just wanted to drop my two cents. I use sessions to track people on my sites with out a database and I do count the number of session files in the session directory to determine the number of active users. But I garuntee that it is correct through two controls. One the directory in which the sessions are stored are only for that site. Two I have a entry that tracks when a user was last active in their session and a Daemon that clears out any session older than 30 minutes in the directory every 30 minutes.

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.