In my case, I want to regularly check my database for expired data, and delete it if it has, so I've added this check to the start of all my PHP web responses. However there's simply no need to do this before the user gets their data, because it's not directly related to them, and I don't want to slow down my website.
I accept that in this case I should probably just schedule a cron task that would run a PHP script, but that doesn’t seem to be a very portable solution. If I migrate servers I would have to manually add this cron task, and it gets even more complicated if I changed platform.
So my two questions are:
- Is it possible to return some data (either an HTML page, or JSON data etc.), and then execute some PHP code in order to ensure the fastest possible response time?
- If not, is there some portable way to schedule a PHP script?