Basically I have a form that accesses a email script I made, and send a message with the content that the user sets in the given fields. I was wondering if it was possible to, through the PHP script, disable it from being submitted more than once...
It's an email form, so if the user presses the "submit" button more than once, it'll send the email more than once. If a person presses the submit button 100 times, 100 messages will be sent to my email.
So, my question is, is there anyway to stop a PHP script from running again once it's already been submitted?
Below is the code of the form.
<form action="http://sebastianalsina.com/contact/sendmail.php" method="post">
<input type="text" placeholder="Name" name="name">
<input type="text" placeholder="Email" name="email">
<input type="text" placeholder="Subject" name="subject">
<textarea placeholder="Write your message here" name="message" rows="6"></textarea>
<input type="submit" name="submit" class="sendmessage" value="Send message">
</form>
Here is sendmail.php:
<?php
require 'PHPMailerAutoload.php';
include 'variables.php';
// receiver message
if ($_POST['name'] != "" && $_POST['email'] != "" && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) && $_POST['message'] != "") {
$mail = new PHPMailer;
$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->setFrom($fromEmail, $_POST['name'] . ' (' . $companyName . ' Web Mailer)');
$mail->addAddress($toEmail);
$mail->isHTML(true);
$mail->Subject = $_POST['subject'];
$mail->Body = '**CODE WAS REMOVED HERE BECAUSE IT WAS REALLY LONG**';
$mail->AltBody = $_POST['message'];
if(!$mail->send()) {
header("Location: error.php");
} else {
header("Location: thankyou.php");
}
} else {
header("Location: error.php");
}
?>