Home arrow PHP arrow Page 2 - The Quickest Way To Count Users Online With PHP

The Quickest Way To Count Users Online With PHP

We've written more than 1 article on how to count users online, but in this article Elan assures us that he's found the quickest, easiest and most logical method. Take a look and see what you think!

Author Info:
By: Elan Bechor
Rating: 4 stars4 stars4 stars4 stars4 stars / 93
October 04, 2002
  1. · The Quickest Way To Count Users Online With PHP
  2. · Advantages and Disadvantages To This Method
  3. · Deconstructing the Script
  4. · Conclusion

print this article

The Quickest Way To Count Users Online With PHP - Advantages and Disadvantages To This Method
(Page 2 of 4 )

Before we can begin the script, we have find out if the session method is the better solution for your web site. This method has several advantages, and a few minor disadvantages.

It doesn't rely on a database: Some people might not have a database available, making the session method the only available choice. Others might want to save CPU cycles for their database driven forum, or want results to show up while the database is down.

Speed of execution: Timing tests have shown that the session method is around 19 times faster than other tested scripts.

Session method took approximately 0.7 ms to execute.
Database method took approximately 14.2 ms to execute.

Conclusion: The session method is roughly 19.4 times faster than the database method!

Right now you might be saying, "You saved 14 milliseconds. Big deal". It is true, with one concurrent user, there is no noticeable difference in speed. But with 50 or 100 people on your site at once, it becomes a strain on the database, leading to slower results. 10 milliseconds can turn into 500 milliseconds, and by then it can make a huge difference.

Built in garbage collector: Using sessions gives us another advantage, which is a built in garbage collector. PHP will get rid of "old" (the session hasnít been accessed within a certain time limit -- by default 1440 seconds) sessions every 100 page requests (can be changed in php.ini). With a database solution, we have to delete old results ourselves, thus slowing the script down even more.

Easy to implement: The sessions method allows a programmer to simply call session_start() to track a user, as opposed to the database method which requires about 15 lines of code.

No detailed statistics: You can hack away at a database visitor counter and be able to add more detailed statistics. An example of this is counting visitors based on the page they're viewing, not the website.

Requires PHP4: If you or your host still haven't updated to PHP4 (very unlikely), then you will not be able to use this method.

Building the Script
The script is surprisingly simple. It's made up of one main function called getOnlineUsers and looks like this:

/* 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 />';

blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials