Hey! i'm looking for some way that can help me in task like this: write a function that every 2 days clear content of XML file. Do this with no CRON or similar tools, just PHP. So far i've never done time-related-run script so is there a way to make it w开发者_JAVA技巧ith only PHP? I assume that everything what functions does is without users ingerention, so i try function sleep(), but i think there must be better solution. Anyone know way to help me?
On every request check modification or creation date of file. If current time minus file time is greater than 2 days - delete file or clear content.
Request in this case is poor resolution clock. Generally checking file time (stat) is quite fast operation.
You might want to look into running your script as a daemon. PEAR's System_Daemon class manages a lot of this functionality. You can take a look at http://kevin.vanzonneveld.net/techblog/article/create_daemons_in_php/ for a good tutorial on how to set up a daemon.
Cron is obviously better for a timed task, but if cron really isn't available, this might be an option.
There are services that call your scripts at given times.
ONLINE CRON SERVICES
Without Cron or leaving your browser open, i'm not sure this is possible?
Letting the script sleep()
for 2 days is no good idea. Use a webcron.
When the reason for no cronjobs is, that you cannot define cronjobs on your server, maybe you can call a script on another (local) machine that makes a HTTP request every other day which fires your PHP script?
If your script is fast to execute you can use an "user based cron".
-You save somwhere the last execution time -Each visitor trigger a check on the last execution time -if the execution time exceed the defined duration the script is executed
It can be a good solution if your script don't need an exact execution timing and is very quick to execute (the user which trigger the execution should not be impacted)
This will run your script for you every 2 days (or specify number of days) http://www.phpjobscheduler.co.uk/
You can schedule the jobs using a MySQL database or a file as described here.
WordPress is also using the same method when cron jobs doesn't available or not been configured.
精彩评论