View Full Version : Online tracker

Jon W
03-22-2009, 11:14 PM
So as some of you may know, I'm working on a text base game right now with my friend. We wanted to be able to find out and show other players how many people are online. First thing I thought of was well, we could have a new field for the users table that is called 'online' if the users online field is set to 1 they are online, if its set to 0 they are offline. Simply by them logging in, it sets the online field to 1, by logging off it sets it to 0... Then I thought.. Well, we are going to have users that says that they are online still because what if they close out the browser without clicking logout...

So, thats my question.. How would I fix something like this? Is it even possible with PHP?

Jon W

Jon W
03-22-2009, 11:27 PM
Maybe.. Not to sure if this would work. But say everytime they login we could store that online session inside of a session..

if($_SESSION['online'] != 1) {

mysql_query("UPDATE users SET online='0' WHERE user_id='".$_SESSION['user_id']."'") or die('Server Error: ' . mysql_error());


The question is will this run really fast before it closes.. lol

03-22-2009, 11:46 PM
No that wouldn't work. Each session key is individual to the user. You would need a way of seeing how many users have a session key in use. You would need a timeout on sessions. You simply count how many session files there are in the session directory. This isn't going to be completely accurate though you'd need a AJAX function to be completely accurate as to who is online. Something like what facebook do. Which is constantly sending data back and forth.

03-22-2009, 11:49 PM
I found you a script :)

/* Start the session */

/* 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) {

return $count;

} else {
return false;


echo 'Number of online users: ' . getOnlineUsers() . '<br />';

Jon W
03-22-2009, 11:57 PM
What type of file would I create to put all of login sessions in?

03-23-2009, 01:04 AM
you don't need to create a file the sessions are automatically stored on the server in session_save_path (http://uk.php.net/manual/en/function.session-save-path.php). Just try the script :-)

Jon W
03-23-2009, 01:08 AM
Oh, okay. I got after I started reading up more about it. lol :P

Thanks for the help. :)

03-23-2009, 01:19 AM
The script i posted does work i tested it. Did you try it?

Jon W
03-23-2009, 01:28 AM
Yes I did.

This was my error that I got:

Parse error: syntax error, unexpected T_ELSE in /var/www/learn/case.php on line 23

I took out the else statement and then I get: Parse error: syntax error, unexpected T_ELSE in /var/www/learn/case.php on line 23

Jon W
03-23-2009, 01:33 AM
But I just fixed that. :P So yes, everything is working good. :)

Jon W
03-23-2009, 02:14 AM
Okay, I should say it did work.. lol Now I'm getting the fulling error.

Warning: fileatime() [function.fileatime]: stat failed for /var/lib/php5\sess_1559ea5f36a55e4a77007528f65f4d07 in /var/www/learn/case.php on line 15
Number of online users: 0

Any clue?

03-23-2009, 01:52 PM
hmm. It works fine for me. maybe the stat if failing because your system doesn't record last access time of file? Is it a linux server or maybe a windows server?

Some things you could try:

1. make a file somewhere on the server HDD. Access it - open it in a text editor or something. Then test
echo fileatime($full_file_path); obviously replace $full_file_path with the full file path of that file.

2. If that doesn't work. Then you'll need to forget the timeout functionality of this function i think. Comment the 'if(...){' and '}' lines

3. If it does work then maybe you need to set session_save_path. Maybe something like session_save_path("/tmp/sessions"); but will need to create that folder on server and set permissions. Where ever you put it don't put it in the doc root of your site. Also you would have to set the session_save_path() on every page that uses your sessions.

try these things and let me know how you get on. If worse comes to worse I might be able to nick something else from E107 later when I'm at home and see how they count online users.