I need some PHP script (that gets feeds) to run periodically on this site I'm supposed to make. How is this done usually? This needs to be set up on an ordinary LAMP hosting service. How would you do this?
One way that comes to mind is simply checking the time when someone visits the site and triggering the script but this seems very inelegant.
How is this kind of thing done usually? Is cron available on most hosting services?
Either move to a new host that supports cron jobs, or, I had an idea for doing something similar once. Store the date of each person that visits your site. Then, the closest match to when you would run your cron job (say, each day) would trigger a script to include the cron job. Now, it's a bit hacky, I'm not even sure if it is technically feisable, but, a couple of years ago, when I was just thinking up of some really odd solutions without finding out the proper way, it's something I came up with. I never tried it, though.
Store the time of day that you want the script to run. Each time someone views a page, check whether that time has passed already. If so, run the "cron" script after sending the response to the visitor's request. Nobody's going to care whether the application data is up-to-date until someone actually views a page, right?
You will have to send a Content-Length header to make the visitor's browser disconnect once the response is sent, otherwise they will sit there with an hourglass cursor until the script ends.
You will also have to store a value indicating that the "cron" script has been run for that day so that it doesn't get run repeatedly after the desired run time has passed.