PHP
  Home arrow PHP arrow Page 2 - The Quickest Way To Count Users Online Wit...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

The Quickest Way To Count Users Online With PHP
By: Elan Bechor
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 48
    2002-10-04

    Table of Contents:
  • The Quickest Way To Count Users Online With PHP
  • Advantages and Disadvantages To This Method
  • Deconstructing the Script
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    Advantages
    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.

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

    More PHP Articles
    More By Elan Bechor


       · Your script is great, but there is still one minor detail. When entering my site a...
       · you use session_save_path according to php.net
       · hireally your article is nice and it is also workingbut if you give detailin...
     

    PHP ARTICLES

    - 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
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT