I have a continuous for loop for my php web based program. I use it to update things on the server that need to be updated on a regular bases like every second or 10 seconds or so depending on what it is. The problem I am having with the for loop is that it keeps exiting out for reasons I do not know. I do know that it is not because of set_time_limit because I have that set to 0, and it exits out at random intervals. It taken as long as 2500 seconds to exit out and as short as 65 seconds. It typically ends around 85 seconds. What I find is odd though is that if I get two users to activate the loop, the loop can stay on indefiantly, but if one exits out it exits out of the loop. Also I know my problem is fixable because I had a similar problem about a year ago and I fixed it but I do not remember how. In fact I spent half the night trying to figure out how I did it before. Now the other problem I am having is that I what this script to be remotely activated so someone does not need to keep the window open to keep the script for loop running. I can do this through Cron Jobs, but I would rather not.
<?php
set_time_limit(0);
$function = 1;
require "global_conf.php";
require "serverfunctions.php";
echo "Server Starting";
echo "<br>";
for (;;)
{
$server = mysql_query("SELECT * FROM `server` WHERE `id` = 1")or die(mysql_error());
$server = mysql_fetch_array($server);
//Sever Exit
if ($server['on'] == 0)
{
echo "Server Shutting Down";
break;
}
//Resource Gain
if (is_null($nexttime) OR $nexttime <= time())
{
$nexttime = time() + 1;
echo $nexttime;
echo "<br>";
$stuff2 = mysql_query("SELECT * FROM `junk`")or die(mysql_error());
while ($stuff = mysql_fetch_array($stuff2))
{
$wood = round($stuff['rugs'] + $stuff['rgain'] / 3600,9);
if ($rugs >= $stuff['rlimit'])
{
$rugs = $stuff['rlimit'];
}
//Pop Gain
if (round($stuff['p']) == $stuff['plim'])
{
$pop = $stuff['poplim'];
}
else
{
//Population Growth Equations
}
$update = mysql_query("UPDATE `" . $database . "`.`junk` SET `blah` = '" . $pop . "', `rugs` = '" . $rugs . "' WHERE `junk`.`id` =" . $stuff['id'] . " LIMIT 1;")or die(mysql_error());
}
}
}
?>
BTW, I have the echos there because I plan to have it create an html page that tells me what is going on in this file. It is just there for referral for now. Oh yes and server functions is empty as of this moment.