Anytime I consider using a CRON job, I always try and figure out a way to accomplish the same result without having to rely on such a server intensive method. For example, most of the time the data only needs to be updated if someone wants to view it...
I keep a time file that simply has a unix timestamp from time() and that is considered the last update time. When someone requests a page that contains data that might need to be updated, the script compare the current time to the last update time. If the difference between the two is greater than the refresh rate, the data is updated and then outputted. Otherwise, if the difference is les than the refresh rate, the existing data is used.
Kind of like this:
CODE
<?php
$time_current = time();
$time_updates_last = file_get_contents('time_data.txt'); // gets the contents from a file that only has a single value in it
$refresh = 1800; // 60 seconds / 1 minutes * 30 minutes = 1800 seconds
$data_age = $time_current - $time_updated_last;
if($data_age >= $refresh){ // past refresh time
// Do whatever data refreshing you need to here and then write a new vale to the 'time_data.txt' file
// Then outout the data to the user.
}
else{
// Just output the data to the user.
}
?>
However, if this is a deal where you are doig more than simply updating data for output, then you have more work to do.
For example, imagine that you have a ining rate and your user is able to mine 4 units per hour and you want to update this every 30 minutes. If you used the system above, then the users mined units would only increase by 2 when the refresh rate was reached and someone loaded the page... That means if nobody loaded the page for 4 hours, then that user would only increase his mined units by 2 for the 4 hours...
This would be a problem wouldn't it?
So you need to make sure that the script then accounts for the number of time that the data should have been refreshed.
For example, here is a quick example to determin the number of times the update should have taken place.
CODE
<?php
$time_current = time();
$time_updates_last = file_get_contents('time_data.txt'); // gets the contents from a file that only has a single value in it
$refresh = 1800; // 60 seconds / 1 minutes * 30 minutes = 1800 seconds
$data_age = $time_current - $time_updated_last;
if($data_age >= $refresh){ // past refresh time
$update_count = $data_age / $refresh; // This give you the number of times in decimal form that the update should have happened.
}
else{
// Just output the data to the user.
}
?>
I suggest that you round this number "DOWN" to the nearest interger 3.7654 becomes 3 etc...
Do what ever update you need on the data and use the update count to determin the number of times to multiply that or perform a loop.
When you update the stored timestamp in 'time_data.txt' don't use the $time_current as that will throw off your interval by whatever the fraction was that you removed earlier. Instead, use ($refresh * $update_count) + $time_updates_last;
This will make sure that you only update every 30 minutes not 24, 29, 20, 10, 18 minutes.
I mentioned a loop...
If your values build on each other like compounding interest, then you have to use a loop like so:
Increase the user's gold by 25% each cycle:
CODE
for($x = 1; $x <= $update_count; $x++){
$gold = $gold * 1.25;
}
So if $gold equaled 10 and the update count was 3, then the following would result:
$gold = 19.53125;
10 * 1.25 = 12.5
12.5 * 1.25 = 15.625
15.625 * 1.25 = 19.53125
The same result can be found without a loop like so:
CODE
$gold = $gold * (1.25^$update_count);
Now this would only affect the server when someone loads a page otherwise, the server is happy to ignore the script. This saves the server from performing calulations when it isn't really needed.
Your web host will realy appreciate you efforts with this...
vujsa
Hey vujsa, thanks for this information, you don't know how much time i will save with this nice piece of code.