diff --git a/contact.php b/contact.php index d98bac7..e161b0e 100644 --- a/contact.php +++ b/contact.php @@ -10,36 +10,33 @@ include_once $URlcorrection . "includes/header.php"; include_once $URlcorrection . "includes/banner.php"; include_once $URlcorrection . "includes/nav.php"; -// Generate CAPTCHA numbers -$num1 = rand(1, 10); -$num2 = rand(1, 10); -$captchaAnswer = $num1 + $num2; +// Generate a new CAPTCHA question if not set +if (!isset($_SESSION['captcha'])) { + $_SESSION['captcha'] = [ + 'num1' => rand(1, 10), + 'num2' => rand(1, 10), + 'answer' => $_SESSION['captcha']['num1'] + $_SESSION['captcha']['num2'] + ]; +} -if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit'])) { +if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = trim($_POST['name']); $email = trim($_POST['email']); $phone = trim($_POST['phone']); $subject = trim($_POST['subject']); $message = trim($_POST['message']); $captcha_input = isset($_POST['captcha']) ? (int)$_POST['captcha'] : 0; - $submittedAnswer = isset($_POST['captcha_answer']) ? (int)$_POST['captcha_answer'] : null; - if ($captcha_input !== $submittedAnswer) { - $errorMessage = "Incorrect CAPTCHA answer. Please try again."; - // Store form data for re-population - $storedName = $name; - $storedEmail = $email; - $storedPhone = $phone; - $storedSubject = $subject; - $storedMessage = $message; + if ($captcha_input !== $_SESSION['captcha']['answer']) { + echo "
Incorrect CAPTCHA answer. Please try again.
"; } else { - $to = " enquiries@warmseal-roofing.co.uk"; - $website_name = "Warmseal contact"; + $to = "enquiries@warmseal-roofing.co.uk"; + $website_name = "Warmseal Roofing contact"; $from_email = "noreply@" . $_SERVER['HTTP_HOST']; $headers = "From: " . $website_name . " <" . $from_email . ">" . "\r\n" . "Reply-To: " . $name . " <" . $email . ">" . "\r\n" . "X-Mailer: PHP/" . phpversion(); - $body = "You have received a new message from Bedford Wealth Contact form:\n\n" . + $body = "You have received a new message from Warmseal Roofing Contact form:\n\n" . "Name: " . $name . "\n" . "Email: " . $email . "\n"; if (!empty($phone)) { @@ -49,28 +46,19 @@ if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit'])) { "Message:\n" . $message; if (mail($to, 'New Message from ' . $website_name, $body, $headers)) { - header("Location: submission"); - exit(); + echo "
Thank you for contacting us. We will get back to you shortly.
"; } else { - $errorMessage = "There was an error sending your message. Please try again."; - // Store form data for re-population - $storedName = $name; - $storedEmail = $email; - $storedPhone = $phone; - $storedSubject = $subject; - $storedMessage = $message; + echo "
There was an error sending your message. Please try again.
"; } } -} else { - // Initialize stored data as empty on initial load - $storedName = ''; - $storedEmail = ''; - $storedPhone = ''; - $storedSubject = ''; - $storedMessage = ''; - $errorMessage = null; -} + // Generate a new CAPTCHA for the next submission AFTER processing the form + $_SESSION['captcha'] = [ + 'num1' => rand(1, 10), + 'num2' => rand(1, 10), + 'answer' => $_SESSION['captcha']['num1'] + $_SESSION['captcha']['num2'] + ]; +} ?>