What is a practical use for PHP's sleep()?

前端 未结 13 2521
时光说笑
时光说笑 2020-12-23 19:17

I just had a look at the docs on sleep().

Where would you use this function?

Is it there to give the CPU a break in an expensive function?

Any common

相关标签:
13条回答
  • 2020-12-23 20:04

    A quick pseudo code example of where you may not want to get millions of alert emails for a single event but you want your script to keep running.

      if CheckSystemCPU() > 95  
            SendMeAnEmail()
            sleep(1800)
      fi
    
    0 讨论(0)
  • 2020-12-23 20:05

    You can use sleep to pause the script execution... for example to delay an AJAX call by server side or implement an observer. You can also use it to simulate delays.

    I use that also to delay sendmail() & co. .

    Somebody uses use sleep() to prevent DoS and login brutefoces, I do not agree 'cause in this you need to add some checks to prevent the user from running multiple times.

    Check also usleep.

    0 讨论(0)
  • 2020-12-23 20:05

    Another way to use it: if you want to execute a cronjob more often there every minute. I use the following code for this:

    sleep(30);
    include 'cronjob.php';
    

    I call this file, and cronjob.php every minute.

    0 讨论(0)
  • 2020-12-23 20:08

    Among the others: you are testing a web application that makes ayncronous requests (AJAX calls, lazy image loading,...)

    You are testing it locally so responses are immediate since there is only one user (you) and no network latency.

    Using sleep lets you see/test how the web app behaves when load and network cause delay on requests.

    0 讨论(0)
  • 2020-12-23 20:09

    Old question I know, but another reason for using u/sleep can be when you are writing security/cryptography code, such as an authentication script. A couple of examples:

    1. You may wish to reduce the effectiveness of a potential brute force attack by making your login script purposefully slow, especially after a few failed attempts.
    2. Also you might wish to add an artificial delay during encryption to mitigate against timing attacks. I know that the chances are slim that you're going to be writing such in-depth encryption code in a language like PHP, but still valid I reckon.

    EDIT

    Using u/sleep against timing attacks is not a good solution. You can still get the important data in a timing attack, you just need more samples to filter out the noise that u/sleep adds.

    You can find more information about this topic in: Could a random sleep prevent timing attacks?

    0 讨论(0)
  • 2020-12-23 20:11

    One place where it finds use is to create a delay.

    Lets say you've built a crawler that uses curl/file_get_contents to get remote pages. Now you don't want to bombard the remote server with too many requests in short time. So you introduce a delay between consecutive requests.

    sleep takes the argument in seconds, its friend usleep takes arguments in microseconds and is more suitable in some cases.

    0 讨论(0)
提交回复
热议问题