Author Topic: Checking Active Members Online  (Read 137 times)

0 Members and 1 Guest are viewing this topic.

Offline ExoonTopic starter

  • Irregular
    • View Profile
Checking Active Members Online
« on: February 08, 2010, 03:30:27 PM »
Hello,

Ive just finished making a login system for my site. I was wondering how i can check how many members are currently online?

Offline Goldeneye

  • Enthusiast
    • View Profile
Re: Checking Active Members Online
« Reply #1 on: February 08, 2010, 03:37:59 PM »
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.
"When you do things right, people won't be sure you've done anything at all."

Offline ignace

  • Addict
  • Gender: Male
  • doh
    • View Profile
Re: Checking Active Members Online
« Reply #2 on: February 08, 2010, 03:39:00 PM »
Code: [Select]
create table user (
   ..
   last_click_at datetime,

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

Code: [Select]
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?
« Last Edit: February 08, 2010, 03:40:25 PM by ignace »
Computer programming is tremendous fun. Like music, it is a skill that derives from an unknown blend of innate talent and constant practice. Like drawing, it can be shaped to a variety of ends – commercial, artistic, and pure entertainment. Programmers have a well-deserved reputation for working long hours but are rarely credited with being driven by creative fevers. Programmers talk about software development on weekends, vacations, and over meals not because they lack imagination, but because their imagination reveals worlds that others cannot see.

Quote
The box said ‘Requires Windows Vista or better’. So I installed Linux

Quote
Windows comes pre-bundled with IE to download Firefox


Offline Goldeneye

  • Enthusiast
    • View Profile
Re: Checking Active Members Online
« Reply #3 on: February 08, 2010, 03:45:29 PM »
Code: [Select]
create table user (
   ..
   last_click_at datetime,

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

Code: [Select]
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.
"When you do things right, people won't be sure you've done anything at all."

Offline ExoonTopic starter

  • Irregular
    • View Profile
Re: Checking Active Members Online
« Reply #4 on: February 08, 2010, 04:23:43 PM »
Code: [Select]
create table user (
   ..
   last_click_at datetime,

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

Code: [Select]
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?

Offline ignace

  • Addict
  • Gender: Male
  • doh
    • View Profile
Re: Checking Active Members Online
« Reply #5 on: February 08, 2010, 04:42:49 PM »
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
« Last Edit: February 08, 2010, 04:49:29 PM by ignace »
Computer programming is tremendous fun. Like music, it is a skill that derives from an unknown blend of innate talent and constant practice. Like drawing, it can be shaped to a variety of ends – commercial, artistic, and pure entertainment. Programmers have a well-deserved reputation for working long hours but are rarely credited with being driven by creative fevers. Programmers talk about software development on weekends, vacations, and over meals not because they lack imagination, but because their imagination reveals worlds that others cannot see.

Quote
The box said ‘Requires Windows Vista or better’. So I installed Linux

Quote
Windows comes pre-bundled with IE to download Firefox


Offline ExoonTopic starter

  • Irregular
    • View Profile
Re: Checking Active Members Online
« Reply #6 on: February 09, 2010, 09:10:23 AM »
I don't really understand this. Is this the simplist way?

Offline ignace

  • Addict
  • Gender: Male
  • doh
    • View Profile
Re: Checking Active Members Online
« Reply #7 on: February 09, 2010, 09:38:36 AM »
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:

Code: [Select]
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).
« Last Edit: February 09, 2010, 09:50:37 AM by ignace »
Computer programming is tremendous fun. Like music, it is a skill that derives from an unknown blend of innate talent and constant practice. Like drawing, it can be shaped to a variety of ends – commercial, artistic, and pure entertainment. Programmers have a well-deserved reputation for working long hours but are rarely credited with being driven by creative fevers. Programmers talk about software development on weekends, vacations, and over meals not because they lack imagination, but because their imagination reveals worlds that others cannot see.

Quote
The box said ‘Requires Windows Vista or better’. So I installed Linux

Quote
Windows comes pre-bundled with IE to download Firefox


Offline ExoonTopic starter

  • Irregular
    • View Profile
Re: Checking Active Members Online
« Reply #8 on: February 09, 2010, 09:42:14 AM »
Ok That is alot clearer. Thank you.

Offline WolfRage

  • Devotee
  • Gender: Male
    • View Profile
    • Feral Bytes
Re: Checking Active Members Online
« Reply #9 on: February 09, 2010, 10:04:00 AM »
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.
-- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * --
Please be forewarned; rather than giving you exactly what you want I prefer to teach you how to get what you want. Knowledge is power, so take the time to learn PHP and you will be able to wield it's power.
If I just gave you the code to solve your problem then you will be back again tomorrow asking for more of the same code. So please take the time to learn. Thanks.

PHP Freaks Forums

« on: »

Tired of these ads? Purchase a supporter subscription to get rid of them.