How to limit a command for time? Php

Asked

Viewed 90 times

0

I wish this function of my php, could only be used every 3 minutes, how do I do this?

Command is this.

case strpos($mensagem, '/metar') === 0:
            if(strlen($mensagem)>6 && strlen($mensagem)<=11){
                $airport = substr($mensagem, 7);
                sendParse($chatid, $met->icaoMetar($airport)."\n \n".$met->icaoTAF($airport)."\n\n @OneThousandChat");
            }
  • Global or per user? can record in the session or in the database, and make a validation before making the call, so I know the only way is this.

  • Global, has no database.

  • It depends on what its function does, if it is a loop that rotates infinitely it may be possible to implement, otherwise it will be difficult.

  • Negative, command is executed, then it shows the information, it will only show again when command is executed again

  • You can’t assume anything if we don’t know how the code works, you’re just stating because you know what you did, what I said is based on what you can understand, and there’s a detail, PHP is not asynchronous, so some kind of operation will have to occur, or recursion or loop to get, or you just want a check, but the way this one can not tell what is the solution to your case.

1 answer

1


You can try doing it this way:

if (! $_SESSION['dh_acesso'])
{
    $_SESSION['dh_acesso'] = new DateTime();

    $permite = TRUE;
}
else
{
    $dh_atual = new DateTime();

    if ($dh_atual->diff($_SESSION['dh_acesso'])->i > 3)
    {
        $permite = TRUE; // Passou 3 min., então pode permitir...

        unset($_SESSION['dh_acesso']); // Esvazia a sessão para um novo ciclo
    }
    else
    {
        $permite = FALSE;
    }
}

if ($permite)
{
    // Chama a função aqui...
}

On the first attempt, the $_SESSION['dh_acesso'] which will store the date and time of access and the flag $permite will be defined as TRUE. Then your function can be executed.

On the second attempt, the else shall be carried out, that is, the validation of the time $_SESSION['dh_acesso']. Therefore, if the 3 minutes have been exceeded, your function can be executed and the session will be emptied to start a new cycle. Otherwise, the execution will be prevented.

Remember to give the session_start() and configure the date_default_timezone_set() correctly.

It’s an idea. :)

Browser other questions tagged

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