0

I want to be able to run php script and then it should check itself to make sure X number of seconds have passed before it can run again. I am not talking about halting it's execution for x number of seconds using sleep() procedure.

For instance, a user clicks on a link to execute a PHP script. That php script shouldn't be allowed to run again until it has finished its execution and/or x number of seconds have passed, even if the user clicks the link again real quick like within a second.

I was thinking of creating a time difference flag and check it every time PHP script is called, but it doesn't seem to be working for me.

php file

//For the first time, it will skip over this block of code, because $timediff is not initialized yet.
if (isset($timediff))
{
   if ((microseconds(true) - $timediff)<10) {
      exit();
}
...
$timediff = microseconds(true);
...

But this doesn't work for me.

Any hints or help will be greatly appreciated. Thanks.

19
  • 2
    You need to store the last execution time and whether the script is running somewhere, in a lock file or database. Commented Jul 19, 2024 at 17:59
  • How is that PHP script getting executed? Is it just a link like /yourserver/path/foo.php? In which case every execution is independent so your $timediff is not shared across runs. "because $timediff is not initialized yet" will always be true. Commented Jul 19, 2024 at 18:02
  • 2
    A normal session will only really work if the user allows you to store a cookie. Robots, for instance, often don't. Commented Jul 19, 2024 at 18:06
  • 1
    Is the "overloading the system" coming from the actual download bandwidth usage? Or is it that you're generating the PDF dynamically and it's computationally expensive to do so? Commented Jul 19, 2024 at 18:35
  • 1
    OK, so the public links to the PDFs, those PHP scripts, are dynamic. Surely you placed the actual PDFs outside the web root? And you made sure, that your assumptions, that an user has to provide all the information, is correct? Anyway, my point about impacting usability if you restrict users to one download is important. If you implement something like this, it should be obvious to the user that the link cannot be used twice, and also what they should do if, for whatever reason, they want to download the PDF again. Commented Jul 19, 2024 at 19:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.