The simplest is to redirect to another page, however, the user can still turn the page and reprocess it.
To provide a greater guarantee, raise a flag using session variable:
if ($libera == "sim" && !isset($_SESSION['submitted'])){
$sql = mysql_query ("INSERT INTO a_finan(id_reserva,id_cliente)VALUES('$id_res','$id_cliente')", $conexao) or die( mysql_error());
$_SESSION['submitted'] = true;
}else{
echo "";
}
On the form page, put:
<?php
if (isset($_SESSION['submitted'])) {
unset($_SESSION['submitted']);
}
This serves to reset the flag and be able to post legitimately from the form if the user wants to register something else, not duplicated.
Starting the session
Of course, you should know that for the use of session variables you need to initialize them with session_start().
I omitted the startup in the above examples as it may be that your system is already using and this could cause some error. If your scripts are not using, simply invoke the function:
On the form:
<?php
session_start();
if (isset($_SESSION['submitted'])) {
unset($_SESSION['submitted']);
}
In the script that receives the data and writes:
session_start();
if ($libera == "sim" && !isset($_SESSION['submitted'])){
$sql = mysql_query ("INSERT INTO a_finan(id_reserva,id_cliente)VALUES('$id_res','$id_cliente')", $conexao) or die( mysql_error());
$_SESSION['submitted'] = true;
}else{
echo "";
}
Let’s complicate the matter?
The user can still backward and forward (retouch and forward) through the browsing history. In this case, the user is kidding. But it could be legitimate. It is possible to implement more reinforcement in these cases using the session variable, for example, by checking whether the data sent is exactly the same as previously sent.
Use advantage
This is not the case for the question here, but for cases where there is no primary key control to be inserted, it would not be feasible to use the database search technique before inserting because without a single key it would make the search query more complex and often inaccurate.
Example of situation: a table with a primary key of the autoincrement type.
The session technique also helps prevent actions from bots and spammers as a session must be started on the form’s original page. It doesn’t mean it’s 100% safe from bots or spammers, but it helps to make such actions more difficult.
Additional notes
A session variable expires, but still better than having nothing. In addition a session can be set to expire in a time greater than 1 hour. One day for example. For even greater reinforcement, you can use $_COOKIE
, instead of Session. Then let’s hear that the user can delete the cookie. Ok, but then it’s no longer an ordinary user and it’s clear the bad intention. For such a user, you can put whatever block you find a way around. In these cases it is good to guard against suspicious activities and take appropriate measures. At this point we entered into a parallel discussion, which we should not add here because it would deviate much from the main focus, branching out to various subjects.
Maybe it’s a duplicate of Form inserting twice in bank F5
– rray
Definitely a duplicate. The 3 answers there are almost identical to the one we posted here. Funny that the reply from @Bruno Augusto shows how to do with Session, the same that I showed here. But there is no negative. rsrs..
– Daniel Omine