The fastest way to see how many people are online at your site
Recently after a server crash, I wanted to know which of my sites have the most users online. Knowing that information would help me to determine which site is having the extra traffic, but what is the faster way to do this?
There are several ways to see how many people are browsing your site. In my case I wanted a very fast one that wont produce more overhead to the server, as my server has already been crashed from that traffic peak.
Php in most cases uses files to store session data. In general those files are stored in the /tmp directory (for unix based servers) and this is defined in the session.save_path php.ini directive.
The cool thing about php is that we can have different directives for each site, so we could use a different directory for each site. This way our site specific directory will hold one file for each session, and without any extra overhead we will be able to know how many people are browsing the site.
In fact this tweak will have better performance, as there will be less files for php to handle, and it is more secure. The last one is because if all sessions are stored in the same directory then it is possible for someone to hijack a session from another domain.
Let's play with the sessions
To do this we will first need to create a directory which will hold the files. The important thing on this is to know the directory path in the filesystem. So if the directory is under our home dir, then we will need to know the path /usr/home/sessions This directory should have write permissions, so to be sure you can chmod it to 755.
Now we need to change some php directives in order to make this work. If your host provides you with a php.ini file for your site, then you should open it and edit it as follows: