Page 1 of 1

Loop Problem

Posted: Wed May 25, 2005 2:42 pm
by thomas777neo
I have a loop that checks a file for a value, if the value is 1, do something:

Code: Select all

if (file_exists($file_name))
{
	while (true)
	{
		$read = file($file_name);
		$read = $read[0];
		
		if ($read == 0)
		{
			sleep(5);
		}
			else
			{
                        echo "Worked!!!";
                     }
       }
}
Which works fine, as soon as I update the value of 0 in the file to 1, it shows the text.

The problem is that if I have launched the page, I cannot go anywhere else on my site, it just hangs. And if the file value was indeed 1, and I refresh the page. It executes the php before the html is shown, so the user would see the previous html and not the new html.

What can be done to get around this problem?

Posted: Wed May 25, 2005 2:55 pm
by siefkencp
PHP will always process at the server every time the page is called. It is also procedural by nature, though we are working to get past that so its procedural nature will make it more difficult since you can not run a stateless enviroment.

To prevent the looping effect from being seen on the browser, a refresh with a session variable might be in order.

You may also consider using sockets.

Hope my thoughts help,

Chris