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.

    Ok, I figured out the first problem. I need to add a sleep timer at the end of the loop. It must of been maxing out the memory or something with the loop going that fast, but I was wondering if someone could help me make this php file be activated by a 'fake' user so I do not have to go through cron jobs or constantly leave the page open.

      hm..
      I dunno how to do it. but.. Why not use CRON. THat is what it was made for, after all.. Schedule actions without interaction

        It is more a preference, it is easier and quicker if I make a page that starts it up rather than using cron jobs.

          ? Clearly not: If you asked someone to help you set up a cron job, you'd probably already have an answer 😃

          Anyway.. Good luck!!

          Cheers

            Well thank you for you help, I was just hoping that someone would show me how to use an exec() or something along those lines to do it.

              Write a Reply...