franko75 Posted May 21, 2010 Share Posted May 21, 2010 Hi, I want to set up a small php script which simply returns a string, either from an array or from a line in a text file. As well as returning the string any time this script is called, I also want it to automatically cycle through the set of items (either an array or each line in a text file) on a timed basis. For example, every 15 minutes I want the script to step through each item in the list. This script is part of an application which will be run on a LAN in a controlled space. What I'm unsure about is whether or not i can use php's time functions (like sleep()) to continue manipulating the contents of a file over a fixed period of time (3 hours). The script will also be getting called by a client app (Adobe Director) every 30 secs or so, where it will be returning the current element from the file. I've not actually done anything like this before, so i'd be interested to hear any advice. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/202488-using-php-to-iterate-through-a-text-file-on-a-timed-interval/ Share on other sites More sharing options...
trq Posted May 22, 2010 Share Posted May 22, 2010 PHP and the web servers it runs on are completely stateless and therefore have no way of executing script a given time. Depending on how you want your script to work you could either setup the timer in Javascript (on the client) and have the web browser make a new request every 15 minutes. Or you could schedule the script to be execute by your OS's schedule daemon 9this of course would not be seen by any client). Quote Link to comment https://forums.phpfreaks.com/topic/202488-using-php-to-iterate-through-a-text-file-on-a-timed-interval/#findComment-1061859 Share on other sites More sharing options...
franko75 Posted May 24, 2010 Author Share Posted May 24, 2010 Thanks for the reply Thorpe. Although PHP is stateless, I thought it would have been possible to trigger the sleep function to periodically update a file, i.e via a loop where an update is made to the file, then the script sleeps for 15 minutes, then continues into another iteration of the loop. Having said that, it would be a pretty messy and inefficient way to do this - as you say, a cron job would be the best suited for this I think. Quote Link to comment https://forums.phpfreaks.com/topic/202488-using-php-to-iterate-through-a-text-file-on-a-timed-interval/#findComment-1062495 Share on other sites More sharing options...
ignace Posted May 24, 2010 Share Posted May 24, 2010 Thanks for the reply Thorpe. Although PHP is stateless, I thought it would have been possible to trigger the sleep function to periodically update a file, i.e via a loop where an update is made to the file, then the script sleeps for 15 minutes, then continues into another iteration of the loop. Having said that, it would be a pretty messy and inefficient way to do this - as you say, a cron job would be the best suited for this I think. Apparently you don't understand how this all works. Your web server creates a new session for each client which means that for each client your script will start from 0, so if 100 clients connect you will have 100 scripts waiting 15 minutes with different delays until a script executes every second and your server crashes due to resource depletion. They are stateless, they are not aware of a previous started process. In this case you can only introduce state by using a database of some sort. Quote Link to comment https://forums.phpfreaks.com/topic/202488-using-php-to-iterate-through-a-text-file-on-a-timed-interval/#findComment-1062503 Share on other sites More sharing options...
franko75 Posted May 24, 2010 Author Share Posted May 24, 2010 I've probably not explained this very well, but there would only be one client accessing this specific script. It's going to be a controlled environment set up in a LAN and only running for 3 hours, so it's a fairly unusual set up, which is why I was unsure of the best way to handle it. Quote Link to comment https://forums.phpfreaks.com/topic/202488-using-php-to-iterate-through-a-text-file-on-a-timed-interval/#findComment-1062511 Share on other sites More sharing options...
ignace Posted May 24, 2010 Share Posted May 24, 2010 You have explained it very well, 100 clients or 1 client. If this 1 client accesses this script n times, then you have n times running this script. Quote Link to comment https://forums.phpfreaks.com/topic/202488-using-php-to-iterate-through-a-text-file-on-a-timed-interval/#findComment-1062526 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.