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!
The Quickest Way To Count Users Online With PHP (Page 1 of 4 )
Many websites, especially ones using portal scripts or forums, display how many users are currently visiting the website. Displaying "There are currently 202 visitors online" (the most we’ve ever had online at once here at DevArticles) on your website can impress visitors and potential advertisers. But you must remember, if you're going to try and implement a method of counting visitors, you should strive to do it efficiently. This is why this article was written.
Many articles have been written on the subject of counting visitors, but they all rely on a database that has to be updated every time a user visits a new page. It can become a huge stress on the server (especially MySQL, which doesn't handle write accesses as well as other RMDBS's) if your site receives a lot of visitors. Luckily, there is a more efficient, clean, and faster solution to this problem.
Instead of relying on a database, an alternative method simply counts how many session files there are in the session directory. Because this method never has to read through any files, just their file names, this can take less than a millisecond (seven tenths, to be exact) to complete.
This article assumes that the user has intermediate experience with PHP, and is familiar with the concept of sessions. The server running this script must have PHP 4+ installed, and be compiled with sessions.