How to send only one form every 5 minutes

Asked

Viewed 122 times

2

Well, I wanted to know, how could I get the person to send form only within 5 minutes.

<?php
    $nome  = $_POST["nome"];
$locutor = $_POST["locutor"];
$texto = $_POST["texto"];
$coment = "INSERT INTO `pedidos` ( `nome` , `locutor` , `texto`, `data` , `id`, `num` ) VALUES ('$nome', '$locutor', '$texto', now(), '', '')";

mysql_query($coment);
?>

1 answer

1

You can use the Session to store data about a user’s session.

A simple example could be like this:

session_start();
$date = new DateTime();
$now = $date->getTimestamp();
if (!isset($_SESSION['ultimo_timestamp'])) {
  $_SESSION['ultimo_timestamp'] = $now;
} else {
  if($now - $_SESSION['ultimo_timestamp'] < 5 * 60) die('Ainda não passaram 5 minutos...');
  else $_SESSION['ultimo_timestamp'] = $now;
}

This way you keep in the object $_SESSION the timestamp (in seconds) since the last sending of the form and you give a message if it has not passed 5 minutes.

  • But this php I put together with the Insert?

  • @Kloves must put before INSERT because this code stops execution if you have not passed 5 mimutos. I’m assuming that this PHP page only has the code to do the Insert.

  • Then in case I put an "Else" and put the Insert in?

Browser other questions tagged

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