How to limit a command by time? Php

0

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

This is the command.

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");
            }
    
asked by anonymous 01.12.2017 / 21:21

1 answer

1

You can try doing the following:

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'] will be created, which will store the access date and time, and the flag $permite will be set to TRUE . Then your role can be executed.

On the second attempt, else will be executed, that is, time validation will be done due to $_SESSION['dh_acesso'] . Therefore, if the 3 minutes have been exceeded, its function can be performed and the session will be emptied to start a new cycle. Otherwise, you will be prevented from running.

Remember to give session_start() and set date_default_timezone_set() correctly.

It's an idea. :)

    
02.12.2017 / 13:54