PHP: Simple Active Users Counter
Simple Active Users Counter is a service to record and show number of recent active/online users.
Simple Active Users Counter does not use databases. The information is stored in files, but uses a different strategy to the one in other counters: when a user visits a page in our website, a file is created within a "counters" folder. The name of the file is the IP address of the user. For each user a new file is created, and the modification date of the file will correspond to the most recent activity of that specific user in the website. In case the visitor is not a new one, the file with that specific IP address will be overwritten, so a new 'most recent activity' will be recorded (which corresponds to the last modification date of the file). Recording user's last activity does not require reading information stored in a database or text file.
When counting number of recent visitors, all files in the "counters" folder are checked. In case the file has been created recently (for example in the last 5 minutes; this parameter is customizable) the file will be accounted as an active/online user. On the contrary, if the file is older, it will be deleted.
The procedure described for our Active Users Counter allows recording last activity of users without requiring reading the information in the database. Consequently, the webmaster may record information in all pages in the website, but the number of active users may be shown only in main page or in a limited number of pages, so that the computing and read/write activity will be limited to pages showing the number of active users.
As a consequence, Simple Active Users Counter may be consider as a simple (no databases) and fast (mostly for pages recording active users but not showing their number) service, but also a limited one(only active/online visitors are shown). Webmasters must decide whether showing number of active users is good enough for their websites. If so, this service is a good alternative.
Requeriments and code
To run this service, the following are required: