Reputation: 1122
I am having an issue with my reCaptcha v2 still allowing spam. I generally get about 1 a day up until a few days ago and just over this weekend I got 75 spam messages. I have gone through every post I can find on this site and many others and can't seem to find a fix for this.
Any help would be greatly appreciated.
Thank You very much.
Form submit:
<div class="form-group">
<div class="g-recaptcha" data-sitekey="MYSITE-KEY" data-callback="verifyRecaptchaCallback" data-expired-callback="expiredRecaptchaCallback"></div>
<input class="form-control d-none" data-recaptcha="true" required data-error="Please complete the Captcha">
<div class="help-block with-errors"></div>
</div>
Contact PHP
<?php
require('recaptcha-master/src/autoload.php');
$from = 'Contact form <EMAIL>';
$sendTo = 'Contact form <EMAIL>';
$subject = 'New message from Customer';
$fields = array('name' => 'Name', 'surname' => 'Surname', 'need' => 'Need', 'email' => 'Email', 'message' => 'Message', 'tel' => 'Telephone Number' , 'add' => 'Address' , 'comp' => 'Company Name', 'product' => 'Product');
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon! <br> You\'ll be redirected in about 5 secs. If not, click <a href="Return to site">Here.</a>.';
header( "refresh:5;url=http://www..php" );
$errorMessage = 'There was an error while submitting the form. Please try again later';
$recaptchaSecret = 'SECRET-KEY';
error_reporting(E_ALL & ~E_NOTICE);
try {
if (!empty($_POST)) {
if (!isset($_POST['g-recaptcha-response'])) {
throw new \Exception('ReCaptcha is not set.');
}
$recaptcha = new \ReCaptcha\ReCaptcha($recaptchaSecret, new \ReCaptcha\RequestMethod\CurlPost());
$response = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if (!$response->isSuccess()) {
throw new \Exception('ReCaptcha was not validated.');
}
$emailText = "You have a new message from your contact form\n=============================\n";
foreach ($_POST as $key => $value) {
// If the field exists in the $fields array, include it in the email
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
$customerEmail = filter_var($_POST['email'] ?? null, FILTER_SANITIZE_EMAIL);
if (!filter_var($customerEmail, FILTER_VALIDATE_EMAIL)) {
$customerEmail = null; // Invalid email, set to null
}
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Return-Path: ' . $from,
);
if ($customerEmail) {
$headers[] = 'Reply-To: ' . $customerEmail;
}
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
} catch (\Exception $e) {
$responseArray = array('type' => 'danger', 'message' => $e->getMessage());
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
} else {
echo $responseArray['message'];
}
Contact JS
$(function () {
window.verifyRecaptchaCallback = function (response) {
$('input[data-recaptcha]').val(response).trigger('change')
}
window.expiredRecaptchaCallback = function () {
$('input[data-recaptcha]').val("").trigger('change')
}
$('#contact-form').validator();
$('#contact-form').on('submit', function (e) {
if (!e.isDefaultPrevented()) {
var url = "contact-us.php";
$.ajax({
type: "POST",
url: url,
data: $(this).serialize(),
success: function (data) {
var messageAlert = 'alert-' + data.type;
var messageText = data.message;
var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' + messageText + '</div>';
if (messageAlert && messageText) {
$('#contact-form').find('.messages').html(alertBox);
$('#contact-form')[0].reset();
grecaptcha.reset();
}
}
});
return false;
}
})
});
Upvotes: 0
Views: 162