Reputation:
How would I check in PHP if the exact same data has not been already sent?
This will prohibit users (accidently) sending a contact form twice with the same data on my website!.
Thanks!
Upvotes: 0
Views: 234
Reputation: 323
You can for example serialize the form data and store it in a session variable. Upon receiving form data, first check if the same data is already present in the session. You can also add a random number to the form as hidden input field which is also stored in $_SESSION["rand"]. Upon the first submit, the random number is checked for equality and then removed (or changed) in the session array. This way, a single form can only be used once, upon a second submit, the number will be different and not accepted anymore.
When generating a form:
$_SESSION["rand"] = rand();
And add in the form:
<input type='hidden' name='rand' value='{$_SESSION["rand"]}' />
Upon receiving, check:
if ( $_SESSION["rand"] === $_POST["rand"]) {
$_SESSION["rand"] = false;
// continue with stuff...
Of course, with this layout, a single user can only open a single form at the same time, since opening a second form will overwrite the random number. An even better approach would be to create a individual session key and value combination for each form, thus two forms will not overwrite each other.
Upvotes: 1