count how many users are online

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
jplus2
New php-forum User
New php-forum User
Posts: 87
Joined: Wed May 16, 2012 4:17 pm

Wed Aug 15, 2012 7:39 pm

Good day guys,

I need some help on this, I need to know How many users are online
I have done this already, and its working, I have set up a flag on my table that is
column named online which could either have 1 or 0 value. 1 for online and 0 for offline
everytime a user logs in, the flag is set to 1 and if he/she logs out, the flag is set to 0

now all is good except for one thing

what if the user, closes the browser window then goes to a 1 month vacation
in that case, my code for setting the online flag wouldn't get triggered and in result
the user is seem to be online for 1 whole month.

I think the only way to do this is through ajax

I do really need help on this, tips, anything that can help me is very much appreciated

thank you in advance guys
more power

User avatar
macek
php-forum Active User
php-forum Active User
Posts: 277
Joined: Wed Aug 25, 2010 10:42 am
Contact:

Thu Aug 16, 2012 2:48 am

bad idea ,..

better is to create MySQL table "online_users" ( session_id, date ) and insert row each page is viewed and count how many users ( unique session_id ) was active in last 5 minutes

$sql = "INSERT INTO online_users SET session_id='".mysql_real_escape_string(session_id())."', date='".Date("YmdHis")."'";

...

$sql = "SELECT DISTINCT(session_id) FROM online_users WHERE date>'".Date("
YmdHis", time()-300)."'";
$res=mysql_query($sql);
echo mysql_num_rows($res);

// It can be calculated by one SQL query returns in one row, but I don't have enought time to test something like: SELECT COUNT(DISTINCT(session_id)) AS cnt FROM online_users WHERE ...

NigelRen
php-forum Fan User
php-forum Fan User
Posts: 622
Joined: Fri Aug 05, 2011 9:53 am

Fri Aug 17, 2012 12:17 am

An interesting idea - but not one I have actually implemented as yet would be to use some form of caching.
Basic idea would be to use something like APC which - have a single entry which would cache a list of sessions that have so far used the cache and then a single piece of data, with the session ID as the key, which has an expiry time based on your notional session timeout. As a session is used, it would update the list of sessions data item and then set the dummy value with session ID key and the timeout required.
When you want to see how many sessions are active - read the session list, try and fetch each dummy data item using the session ID as key and if it doesn't exist - don't count the session and remove this from the session list item. This leaves the caching mechanism to 'timeout the sessions' by invalidating expired data items.
Must admit this would need some work to make sure it works reliable, but it saves continuous database access. Wrapping it all up in a class would make it easier as well.

Acesystech
New php-forum User
New php-forum User
Posts: 35
Joined: Mon Aug 20, 2012 10:27 pm
Location: Ernakulam
Contact:

Fri Aug 24, 2012 1:17 am

/* Start the session */
session_start();

/* Define how long the maximum amount of time the session can be inactive. */
define("MAX_IDLE_TIME", 3);

function getOnlineUsers(){

if ( $directory_handle = opendir( session_save_path() ) ) {
$count = 0;
while ( false !== ( $file = readdir( $directory_handle ) ) ) {
if($file != '.' && $file != '..'){
// Comment the 'if(...){' and '}' lines if you get a significant amount of traffic
if(time()- fileatime(session_save_path() . '\\' . $file) < MAX_IDLE_TIME * 60) {
$count++;
}
}
closedir($directory_handle);

return $count;

} else {
return false;
}

}

echo 'Number of online users: ' . getOnlineUsers() . '<br />';
Read more at http://www.devarticles.com/c/a/PHP/The- ... vpPFsXR.99

jplus2
New php-forum User
New php-forum User
Posts: 87
Joined: Wed May 16, 2012 4:17 pm

Tue Aug 28, 2012 5:37 pm

Thanks for the respones guys

Acesystech thank you for the code you've posted
But I guess the code counts all users online, diregarding who are the users online
anyways the code give me an idea that we can do use the session folder on the web server to accomplish such task, thank you very much

I just want to clarify that the main goal is to identify if user 1 is online or not

I am just digesting the responses above, my nose is still bleeding, am only a php beginner

if you guys can paste in a sample code, it is much appreciated

once again thank you very much

Post Reply