Exoon Posted February 8, 2010 Share Posted February 8, 2010 Hello, Ive just finished making a login system for my site. I was wondering how i can check how many members are currently online? Link to comment https://forums.phpfreaks.com/topic/191406-checking-active-members-online/ Share on other sites More sharing options...
Goldeneye Posted February 8, 2010 Share Posted February 8, 2010 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. Link to comment https://forums.phpfreaks.com/topic/191406-checking-active-members-online/#findComment-1009045 Share on other sites More sharing options...
ignace Posted February 8, 2010 Share Posted February 8, 2010 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? Link to comment https://forums.phpfreaks.com/topic/191406-checking-active-members-online/#findComment-1009047 Share on other sites More sharing options...
Goldeneye Posted February 8, 2010 Share Posted February 8, 2010 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. Link to comment https://forums.phpfreaks.com/topic/191406-checking-active-members-online/#findComment-1009056 Share on other sites More sharing options...
Exoon Posted February 8, 2010 Author Share Posted February 8, 2010 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? Link to comment https://forums.phpfreaks.com/topic/191406-checking-active-members-online/#findComment-1009085 Share on other sites More sharing options...
ignace Posted February 8, 2010 Share Posted February 8, 2010 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 Link to comment https://forums.phpfreaks.com/topic/191406-checking-active-members-online/#findComment-1009101 Share on other sites More sharing options...
Exoon Posted February 9, 2010 Author Share Posted February 9, 2010 I don't really understand this. Is this the simplist way? Link to comment https://forums.phpfreaks.com/topic/191406-checking-active-members-online/#findComment-1009398 Share on other sites More sharing options...
ignace Posted February 9, 2010 Share Posted February 9, 2010 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). Link to comment https://forums.phpfreaks.com/topic/191406-checking-active-members-online/#findComment-1009406 Share on other sites More sharing options...
Exoon Posted February 9, 2010 Author Share Posted February 9, 2010 Ok That is alot clearer. Thank you. Link to comment https://forums.phpfreaks.com/topic/191406-checking-active-members-online/#findComment-1009408 Share on other sites More sharing options...
WolfRage Posted February 9, 2010 Share Posted February 9, 2010 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. Link to comment https://forums.phpfreaks.com/topic/191406-checking-active-members-online/#findComment-1009415 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.