I'm sorry, but running a cron job every minute is just as bad as running that with every page load. The point of using SQL is that it is quicker than running the cron job. What if your site isn't receiving any traffic at all? Are you just going to keep running and wasting resources on a cron job? If you are using Smarty or Template Lite, or any templating system with caching, then the solution is built in without any need to hit the MySQL server or using a cron job.
But I'm a man of solutions and here is one.
Code: Select all
$count = 0;
if(!isset($_SESSION['last_update']) or ($_SESSION['last_update'] < strtotime("-1 minute"))
{
$_SESSION['last_update'] = time();
// Above Mysql data
$SESSION['members'] = $array[0];
}
else
{
$count = $SESSION['members'];
}
echo $count;
Now this code will only hit the server every minute instead of every page load.
Here is what you do:
1. Create an array that holds, the guests, members, and administrators online.
2. Serialized that array
3. Output the array to a file with file_put_contents()
4. Use filemtime() to get the time of which it was modified.
5a. If it was modified under a minute ago, then grab the contents.
6a. Unserialized the array from the file with file_get_contents().
7a. Populate the main array with the array from the file.
5b. If the file was modified over a minute ago, then ignore the contents.
6b. Grab the information from the mysql database.
7b. Repeat step 2 and 3
8b. Populate the main array.
Cron Job
From your specifications you have stated that you wouldn't mind hitting the mysql database within a new table. I don't see how it is any different from my example, except it is smarter.
But lets for example say that you wrote it to a file instead (better idea for small amounts of data). On the off hours you would run the script for no reason at all running transactions to the database for no reason.
Mysql Transaction on every request
I really don't see the point of why this is such an god awful idea. I do on most of the scripts I write and unless you are querying the database for every little thing, I don't see how the overhead would effect the over processing time. You can optimize the SQL further. When you said you didn't want to query every row, I thought you were using a While loop, so my bad for misunderstanding your level of skillz.
This is by far the easiest, simple method and I think you underrate it just a little bit. "No KISSing for you Mr Santos, you sick freak you!"
My Second Example
Benefits from both worlds of the cron job and my first method. The complexity goes through the roof, but should only take less than 15 minutes to implement if you are a l33t coder or have used the functions before.
Since the cron job isn't working, use my second method. If you can't (or don't) want to code it, then I can provide the code for you, but I will probably do it based on what I know and it could have some bugs that would need to be worked out. As I have never done any serialization, you'll have to excuse that bit... but seriously, how hard can it be? PHP does all the work for you.