Navigation
Poll
Would you be an active poster if Ron's Guide had a message board?


Total Votes: 62
Comments: 18 — View
Past pollsPoll idea?
Rate Ron's Guide
Rate our resource at Bigwebmaster.com
Visitors Online
Learn how to track and display the number of users currently browsing your website. Similar to the users online statistics displayed here on Ron's Guide.

The next part of our script will deal with idle users. We need to manually remove all idle users from our "visitors" array in order to make our online counter work correctly.

This is where recording the timestamps of our visitors comes in. We'll use this timestamp to calculate whether a user is considered "idle" or has left your site altogether.

Add the code below to your online.php file between lines 15 and 18:

// this is the number of seconds after
// which a user is considered "idle"
$timeout 300// 300 seconds = 5 minutes

foreach($online['visitors'] as $key => $val) {
    if(
$val < (time() - $timeout)) {
        unset(
$online['visitors'][$key]);
    }
}

Lines 5 through 9 of the snippet above is a foreach loop. What this is does is iterates through each item in our "visitors" array. Line 6 tests the timestamp of each visitor against the current timestamp minus the number of seconds in our timeout variable. This is what determines if the user is idle or not.

If the timestamp of the visitor is more than 5 minutes old than the script will remove them from the visitors array. You can change the amount of time after which the user is considered idle. Just change the $timeout variable to your liking.

Now that we have that portion of our script complete, all we have to do is update our record number of online visitors, if needed, then echo out the data for all to see.

Add the code below directly below the foreach loop in your online.php file:

// Current number of visitors online
$total_visitors count($online['visitors']);

// Update our record if necessary
if($total_visitors $online['record']['number']) {
    
$online['record']['number'] = $total_visitors;
    
$online['record']['time'] = time();
}

$record_number $online['record']['number'];
$record_time $online['record']['time'];

echo 
'Current visitors online: 
'
.$total_visitors.'.<br />';

echo 
'The most visitors ever online was '
.$record_number.' on '.gmdate('F jS, Y'$record_time);

This is pretty self-explanatory, but I'll go over it anyway. Lines 5 through 8 updates the record number of visitors online if record is broken. Lines 13 through 17 echo out the current number of visitors online along with the record information.

And that's all there is too it. That wasn't too hard, was it? To make sure you got everything correct, below is a copy of the script in its entirety.

<?php

// This is the IP address of our visitor
$ip_addr $_SERVER['REMOTE_ADDR'];

// Once again, edit this variable
// to the path of your online.dat file
$file '/path/to/online.dat';

// Get our array from the online.dat file
$online file_get_contents($file);
$online unserialize($online);

// Update the entry for this visitor
$online['visitors'][$ip_addr] = time();

// this is the number of seconds after
// which a user is considered "idle"
$timeout 300// 300 seconds = 5 minutes

foreach($online['visitors'] as $key => $val) {
    if(
$val < (time() - $timeout)) {
        unset(
$online['visitors'][$key]);
    }
}

// Current number of visitors online
$total_visitors count($online['visitors']);

// Update our record if necessary
if($total_visitors $online['record']['number']) {
    
$online['record']['number'] = $total_visitors;
    
$online['record']['time'] = time();
}

$record_number $online['record']['number'];
$record_time $online['record']['time'];

echo 
'Current visitors online: 
'
.$total_visitors.'.<br />';

echo 
'The most visitors ever online was '
.$record_number.' on '.gmdate('F jS, Y'$record_time);

// Write the new data back to our file
$handle fopen($file'w');
fwrite($handleserialize($online));
fclose($handle);

?>

I hope this tutorial was helpful. If there is any part that you didn't understand or if you have any questions, feel free to use the discussion board.

« Previous [ 1 2 3 ] Next »
Discuss Tutorial: Visitors Online 3 Comments
Comment by Peter on Mar 15, 2006, 9:26 am
Nice one Ron Smile
Comment by Dexxaboy on Mar 17, 2006, 11:35 pm
*Thumbs up* Smile
Comment by Leokingofthelion on Mar 22, 2006, 1:14 am
I'm using it woth my website! Keep the tutorials coming!Tongue

« Previous [ 1 ] Next »
Post a comment
Sorry, you must be a registered member to post comments.

If you would like to register, you can do so here.
If you already have an account, please login.