As a developer, you should always think of the worst possible scenario and always take into account the inexperience (to be nice) of users.
You may have some JS that interrupts some repeat action of that request (F5, for example), but you should also consider that user who actually has some knowledge in the subject matter and for example navigates with minimum JS enabled (like me) which would potentially nullify your alleged "protection".
And this is where the server-side check comes in. You have two options and both can be implemented in parallel:
Accept multiple inserts, but allow the user to view transactions made and, if applicable, remove some duplicity on their own, as in a Shopping Cart that has an overview of orders before making the purchase.
Distinguish if a request is a resubmission or a reload by storing in a Session a hash of all form information and only enter in fact if it is really a submission.
This second alternative is very simple, just store in the session an MD5 hash of everything you have in the form:
<?php
session_start();
if( $_SERVER['REQUEST_METHOD']=='POST' ) {
$hash = md5( implode( $_POST ) );
if( isset( $_SESSION['hash'] ) && $_SESSION['hash'] == $hash ) {
// Refresh! Não faz nada ou re-exibe o formulário preenchido
} else {
$_SESSION['hash'] = $request;
// Submissão legítima! Insere ;)
}
}
?>
<form action="" method="post">
<input type="text" name="field" value="value" />
<input type="submit" name="send" value="send" />
</form>
Just after you submit the form and press F5, does it duplicate the data? If that’s the case, it’s not about your code, it’s about the native process of browsers submitting a form.
– CesarMiguel
You could show us the executed SQL code when submitting the form?
– Paulo Roberto Rosa
I understand Cesar Miguel. At any time of the processing, if I press F5, the data will duplicate... :\
– Marcony Felipe
Unfortunately not Paul. But it would be an Insert into common SQL.
– Marcony Felipe
Finished operation? was successfully performed? redirect the user to a successful page and clear SESSION and CACHE if you use them
– user4724
try adding the autocomplete="off" property to the < form element >
– user5368