0

I want to stop the ability to spam the contact form by refreshing the page. Yes, I tried several methods mentioned here to avoid this and it did not work. Well, partially worked. I use $_POST and I point to the same page (index.php) on form submit it redirects to the same page and then prints success or error message. I manage to fix the refresh spam thing, but then I lose the success/error messages too. Help me to solve it so you can't spam by refreshing the page and still see if message was sent successfully or not. Here's my code:

<?php
if(isset($_POST['submit'])) 
{

$message=
'Full Name: '.$_POST['fullname'].'<br />
Subject:    '.$_POST['subject'].'<br />
Phone:  '.$_POST['phone'].'<br />
Email:  '.$_POST['emailid'].'<br />
Comments:   '.$_POST['comments'].'
';
require "PHPMailer-master/class.phpmailer.php"; //include phpmailer class

// Instantiate Class  
$mail = new PHPMailer();  

// Set up SMTP  
$mail->IsSMTP();                // Sets up a SMTP connection  
$mail->SMTPAuth = true;         // Connection with the SMTP does require 
authorization    
$mail->SMTPSecure = "ssl";      // Connect using a TLS connection  
$mail->Host = "smtp.gmail.com";  //Gmail SMTP server address
$mail->Port = 465;  //Gmail SMTP port
$mail->Encoding = '7bit';

// Authentication  
$mail->Username   = "xxx@xxx.xx"; // Your full Gmail address
$mail->Password   = "xxxxx"; // Your Gmail password

// Compose
$mail->SetFrom($_POST['emailid'], $_POST['fullname']);
$mail->AddReplyTo($_POST['emailid'], $_POST['fullname']);
$mail->Subject = "AL Service Contact Form";      // Subject (which isn't required)  
$mail->MsgHTML($message);

// Send To  
$mail->AddAddress("post@alservice.no", "AL Service"); // Where to send it - Recipient
$result = $mail->Send();        // Send! 
$message = $result ? '<div class="ctrdiv fntsucc"><strong>Meldingen ble sendt! Vi kontakter deg snart.</strong></div><br>' : '<div class="ctrdiv fnterr"><strong>Meldingen ble ikke sendt! Vennligst prøv en gang til.</strong></div><br>';  
unset($mail);

}
?>

<?php if(!empty($message)) echo $message; ?>

0 Answers0