I have a PHP function that I want to make available publically on the web – but it uses a lot of server resources each time it is called.
What I’d like to happen is that a user who calls this function is forced to wait for some time, before the function is called (or, at the least, before they can call it a second time).
I’d greatly prefer this ‘wait’ to be enforced on the server-side, so that it can’t be overridden by dubious clients.
I plan to insist that users log into an online account.
Is there an efficient way I can make the user wait, without using server resources?
Would ‘sleep()’ be an appropriate way to do this?
Are there any suggested problems with using sleep()?
Is there a better solution to this?
Excuse my ignorance, and thanks!
sleepwould be fine if you were using PHP as a command line tool for example. For a website though, yoursleepwill hold the connection open. Your webserver will only have a finite number of concurrent connections, so this could be used to DOS your site.A better – but more involved – way would be to use a job queue. Add the task to a queue which is processed by a scheduled script and update the web page using AJAX or a meta-refresh.