How to detect number of visitors on the entire site?
I was wondering if it was possible to place some php code on each and every page of my content on my site to detect the number of visitors on the site at any one moment just like what PHPNuke is able to do.
It's not difficult but it's not an easy two-line explanation.
You should create a sessions heap table. At the top of every page of your site you include (sessions.php).
In that sessions.php you first query the sessions table to see if this IP address has a current session. If not, you insert into the session table. If it does, you update the table with their current location.
... here's a question: how do you delete old sessions? I've never looked to see how VB does it. It would be too much overhead to clean the session table with each update, so when do you go in and expire the old sessions? Cron job?
I have actually done this before and it takes a bit of genius to actually come up with it, though it is rather simple.
What you do is first gather the user's IP address and shove it into a variable. Then make a variable with a number for your expire time in seconds as your timeout, such as 300 per say. Then make a table called sessions with an ID,IP, and last_time area for the last time it was updated.
Then there are 3 main queries you need to do, INSERT, UPDATE, and DELETE (which is the main garbage collection query)
what you have it do first is check if the IP is IN the database, and if it isint, use an INSERT query to load it in and makesure you use time() to load the unix timestamp. Then put an ELSE for is the users IP does exist, then do an UPDATE and set the users session entry to the new time()
After the IF statement, comes the garbage collection routine, which makes it all automatic. The DELETE query should do the following:
check if the user's time has expired past the expire time (this is where the variable comes in) The math for this shoudl be done in this type of fashion: (time() > (last_time + $expire))
Thats just guidelines, its a fundamental, but it works. The way php functions right now forces you to do automated site functions via the user calling a main script file and doing the raw update routines that way.
Or just parse the server log file... keep count of uniqe IPs until you get to N minutes... no need for a SQL database. If that (or any method) takes too much CPU time, every minute just run via cron to a text file and insert into document using an include.
This should be fairly trivial to implement (and doesn't need SQL!)
I'm not a massive fan of sessions, so I've written my own implementation of them using PHP and mySQL. This allows me to do lots of funky things, including counting the number of users on site at once.
Basically when a user first hits a page you detect that they are a new user because they don't have a cookie set. Create a nice big random string and send it to them as a cookie which expires in say 60 minutes - then save that string in a database table along with a timestamp of the current time.
Each subsequent visit the user makes to a page in your site, have the script detect their cookie and look them up in the database table. If the cookie is there and hasn't expired yet (the script can see if it's expired by adding the expiration time in seconds to the time stamp saved in the database) don't do anything - if they don't have a cookie set one and add it to the database.
To see how many users are on site at once, all a script has to do is count the number of active sessions in the database table (active sessions can be seen by checking for sessions with a start timestamp occuring in the last 60 minutes or whatever).
The downside to this is that if the user has cookies turned off it won't work, so it's a advisable to track their IP as well if you want to use this seriously. I use this system to allow users to log in with their username and password - the session table stores their user ID allowing me to securely check a user's permissions on every page they visit.