Reference Sleep PHP function

Asked

Viewed 813 times

1

I’m looking for a way to limit the use of a script for a certain time, good as said in another question I don’t want to use events mysql.

Searching, researching, trying to do, I found this in the documentation, Sleep Function She says the following:

Sleep - Delay script execution

It’s not well documented as far as I’m concerned, but down below there are a few examples made by contributors.

How this function actually works?

Note: This question is different from this. Limit quantity by time

  • 2

    As the documentation itself says, the function Sleep delays script execution, see this example.

  • In case I could use this function Sleep to limit the use of a button?

  • 1

    No, I think the ideal would be to hide this button because this function will not prevent the user from clicking again on the button.

  • I understand, I’ll see what I can do, but I think that when I click I will have to wait the finished time for me. I hide the button and at the end of this time show me again.

  • 1

    Yes, if you’re using Ajax functioned.

  • Yes I will use ajax, by the way I’m already doing;

  • 1

    What is not written there is that almost every time the function is used is doing something wrong. This is asking to take DOS attack. And if you want to change the behavior of something in the client you have to touch it and not the server. Not that this cannot be circumvented.

  • Research upon research I came to the conclusion that what the miner said is true.

  • Wow brother, why did you remove the question there? It didn’t work?

  • @I thought I’d end up polluting but I’ll reopen

Show 5 more comments

1 answer

2


sleep, as the documentation itself says, its purpose is to delay the execution time of the script, from the point that it is invoked.

Generally, the function is used sleep in infinite loops, usually used running in the background on servers, to perform a certain task.

For example, if you need to check from time to time if something is pending in the database in order to process this data through a Webservice:

// Esse script está rodando pela linha de comando
// Laço (ou loop) infinito para executar uma verificação repetitiva

while (true) {


    $solicitacoes = Solicitacao::where('pendente', '=', 1)->get();

    foreach ($solicitacoes as $solicitacao) {
         $this->processarWebservice($solicitacao);

         $solicitacao->update(['pendente' => 0]);
    }

    sleep(60); // Determina que a próxima iteração sera feita daqui a 60 segundos
}

It is rare to use it on HTTP, but I could already contemplate an implementation of long Polling who wore sleep to delay data query time.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.