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. :)