Home >Backend Development >PHP Tutorial >A simple example of PHP preventing post from repeatedly submitting data_PHP Tutorial
During an interview in a certain empire, I came up with this question: How to deal with duplicate questions in post submissions. Later I communicated with @nuanyang and he said to record the time. I didn’t understand. What I wanted was to use session to record it on the form page. Then submit the page for judgment. If they are equal, it will be regarded as successful and the session will be cleared. But there is a question. What if the form page is HTML? What should I do? How about setting up a PHP verification page? A function similar to the verification code. There are others. It says to use the header to set the expiration time...but I haven't tried it. The following is what I wrote in php, and it has been tested and works.
//If there is a submission identifier
if(isset($_GET['action']) && $_GET['action'] === 'save'){
//If there is a session and it is the same as the passed value, it is considered submitted
if(isset($_SESSION['__open_auth']) && isset($_POST['auth']) && $_SESSION['__open_auth '] == $_POST['auth']){
print_r($_POST);
$_SESSION['__open_auth'] = null;//Clear
} else {
//Get started
header("location: post.php");
}
exit();
}
//Authorization
$auth = $_SESSION['__open_auth'] = time();
?>