Home arrow PHP arrow Page 3 - 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 - Deconstructing the Script
(Page 3 of 4 )

The script begins with call to session_start(), which tells PHP to create a blank file in your sessions directory.

If you don't have a dedicated session directory because you're hosted on a shared server, it isn't a good idea to rely on the default session's directory. Since all of the servers' users share the same sessions directory, you could end up counting files that aren't coming your visitors. To solve this problem, simply add this line at the top of your script:


After this, we define the timeframe (in minutes) that we allow users to be idle for:

define("MAX_IDLE_TIME", 3);

Essentially, if a visitor does not request a new page from the server within this timeframe (in this case 180 seconds), his session file will be ignored and not added to the visitor count.

Hold on. You might remember me saying, "built in garbage collector" earlier, and wondering why this line is necessary. There is, in fact, a built in garbage collector. The only problem with it is that by default, php.ini sets the random chance for the collector to be run at 1%. For smaller sites that don't get much traffic, this could lead to inaccurate results because the script would count old sessions more often than not.

If the Webmaster does not have access to php.ini and cannot change the session.gc_probability value, then this line is necessary.

Next, we begin the getOnlineUsers function. The function begins by opening the session directory. This can be accessed by the built in function session_save_path():

if ( $directory_handle = opendir( session_save_path() ) ) {

The directory handle is given to $directory_handle for further use. If this line is successful, it proceeds.

The next line in the script loops through the directory handle and retrieves the filename:

while ( false !== ( $file = readdir( $directory_handle ) ) ) {

The "false !==" part is needed because if a file is named 0, it will be skipped over. This is because 0 is considered a false Boolean value in PHP, but not exactly a Boolean. With the "false !==" added, 0 would not be mistaken as a FALSE Boolean, since they are not exact.

The next section of code is where the actual counting occurs:

if($file != '.' && $file != '..'){

The current filename in the loop, stored as $file, is first checked to see if it isn't '.' or '..'. This line is needed because when PHP returns a directory, the first two results are always '.' and '..'.

Next, we see the line:

if(time()- fileatime(session_save_path() . '\\' . $file) < MAX_IDLE_TIME * 60) {

This line checks to see if the file has not been accessed in the timeframe that we set up earlier. This line can be commented out if you have access to php.ini or get a fair amount of traffic.

Finally, $count++ increments the counter by one if all of these conditions have been satisfied.

After closing the directory, the function returns the variable $count, which holds the total number of users currently visiting your site.

To begin tracking users, we can simply call session_start() at the beginning of the page. For those of you who have php.ini access, you can also turn session.auto_start to 1.

To display how many users are online, put the getOnlineUsers in a file called getOnlineUsers.php. To display the tally on your site, simply include the file and call getOnlineUsers(), like this:



echo 'There are ' . getOnlineUsers() . ' user(s) currently browsing MyDomain.com';


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-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials