PHP - Updating?
by Josiah Wang · in General Discussion · 01/19/2005 (1:46 pm) · 3 replies
To introduce this topic, I've been developing an open-source web strategy game engine for about two years. AGE (Apocalypse Game Engine) runs on PHP (essential), MySQL (for the database; actually, we're porting it to work on virtually any db right now...), and cron (for the updates). It's basically a 'template' sort of for turn-based strategy games. The current release I'm working on brings it all together into a *real* engine, not just a game (I sorta got the ideas from torque =D).
So, now my question. I've been using cron to update my game files (ie every 5 minutes or so, the updater runs and the players get interest in their bank, more food, etc). However, I want my engine to be hassle-free for those who are not as experienced with servers and do not wish to fool with cron (myself included). I was thinking about a way to update the db through the pages themselves.
Let me try to explain this: right now, every minute, cron uses lynx or wget to *go to* a password-protected page (ie something like update.php?password=secret). That page basically executes all the queries that 'update' the database.
What I'm trying to get it to do is NOT to run cron at all. Instead, at the start of every page, the script will run a check for the last known update. If that timestamp is greater than the $update_second_interval (set by the admin), it will include (execute) a separate script.
Example:
and then, in update.php..
The only question is that i've never seen anything like this in any code I've encountered. Most of the other open-source games i've seen require cron, and don't even have an option to run updates without. Am I missing something here? Does it eat up my code once I run it?
PS: If i shouldn't post here, please let me know x.x
So, now my question. I've been using cron to update my game files (ie every 5 minutes or so, the updater runs and the players get interest in their bank, more food, etc). However, I want my engine to be hassle-free for those who are not as experienced with servers and do not wish to fool with cron (myself included). I was thinking about a way to update the db through the pages themselves.
Let me try to explain this: right now, every minute, cron uses lynx or wget to *go to* a password-protected page (ie something like update.php?password=secret). That page basically executes all the queries that 'update' the database.
What I'm trying to get it to do is NOT to run cron at all. Instead, at the start of every page, the script will run a check for the last known update. If that timestamp is greater than the $update_second_interval (set by the admin), it will include (execute) a separate script.
Example:
<?php
$nowtime = timestamp(); //returns time in unix timestamp format
$last_update_time = last_update(); //returns the timestamp from last update
//$updateinterval is the amount of seconds between updates - set by admin
//in a separate file
if( ($nowtime - $last_update_time > $updateinterval) ) {
include("update.php")
}and then, in update.php..
<?php
//Notice the repetition of checking the time - this is to prevents
//people from doing an update even if they access the file manually
$nowtime = timestamp();
$last_update_time = last_update();
if( ($nowtime - $last_update_time > $updateinterval) ) {
/*
ok, now we really know that this update is VALID. Now to get how many
updates we missed. Since $updateinterval will be in seconds, we can
probably use a division and a set-type to get how many update we missed, and then use a for-loop to execute that many updates.
*/
$updates = ($nowtime - $last_update_time) / $updateinterval;
settype($updates, "integer") //essentially removes the trailing decimals...
for ($i = 1; $i <= $updates; $i++) {
do updates here
}
?>The only question is that i've never seen anything like this in any code I've encountered. Most of the other open-source games i've seen require cron, and don't even have an option to run updates without. Am I missing something here? Does it eat up my code once I run it?
PS: If i shouldn't post here, please let me know x.x
#2
Pseudo-cron.php
How to automate web site updates with PHP
01/20/2005 (2:58 am)
You might find these pages helpful:-Pseudo-cron.php
How to automate web site updates with PHP
#3
This seems like what you want. For calling a script from within a script. $output gives you the output of the called script, if that isn't already obvious. If you really need to, you can modify this to use the POST method instead of GET for passing params.
01/20/2005 (4:31 am)
$header = "GET /yourpath/yourscript.php?yourparam=yourvalue HTTP/1.0\r\n";
$header .= "Host: yourhost.com\r\n\r\n";
$fp = fsockopen ("yourhost.com", 80, $errno, $errstr, 30);
if (!$fp) {
$err="HTTP ERROR:" + $errstr;
} else {
fputs ($fp, $header);
while (!feof($fp)) {
$output .= fgets ($fp, 1024);
}
fclose ($fp);
}This seems like what you want. For calling a script from within a script. $output gives you the output of the called script, if that isn't already obvious. If you really need to, you can modify this to use the POST method instead of GET for passing params.
Torque Owner Pablo Alonso
I hope this helps you out.