Jump to content

Using php to iterate through a text file on a timed interval


franko75

Recommended Posts

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

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.