Reputation: 3025
how data is generated without user activity has never been quite clear to me. Let's say I'm running a lottery and people can check the winning ticket number online. This random number has to be automatically generated at a specific time and should be the same for every visitor. How is that done?
I figured I could use a PHP script which retrieves the previous number and check whether it is, say, over 7 days old. If it is, the script would then generate a new number and store it, so it will be retrieved when other users visit the page, in which case the number would simply be shown as it's not over 7 days old.
This method of doing things might cause trouble with multiple users both trying to access files at the same time or overwriting data which is simultaneously generated by/for another visitor. I think it would be better if the number could be generated and stored independently of user activity, so the PHP script's only task is to read it.
Any ideas would be much appreciated!
P.S: I'm using simple webhosting and do not physically have access to the server, can't leave a .bat or .exe running I guess. Also, tags might be somewhat inaccurate as I don't know what I should be using to tackle this problem.
Upvotes: 1
Views: 138
Reputation: 3298
As you have simple hosting, you're unlikely to have access to the 'preferred' method (scheduled tasks, running a service, etc.) as you say.
So IMO your best option is likely to test the date+time of the last update to the value, and if the right amount of time has passed update it, before you display it.
To address the issue of 'stale information' being displayed to a user who happens to hit the web page moments before it's due to be updated, you could display the date information with the value (lottery numbers?) - e.g. "Numbers for Sat 3 Dec 2011: 1 5 10" so the user knows what they are looking at.
As an extra, you could use javascript on that page to check the date and time and update the displayed value (refresh the page or use ajax) if a user happens to be viewing the page at the time the update is due.
Upvotes: 1
Reputation: 91922
Use some kind of scheduler to execute the task at a given time. One such scheduler is called cron
and is installed in most Unix-like environments.
There are also cloud-based schedulers, which will call an URL of yours at the schedule you choose.
If your hosting provider doesn't provide this service (most do!), try searching for online cronjob
, hosted cron
or similar (a "cronjob" is one job executed by the cron daemon).
Upvotes: 4