У меня есть контактная форма с рабочим reCaptcha, но спам проходит.
Я попытался добавить фильтр спам-слов, который нашел здесь Фильтр спама в контактной форме
, Но это не работает, есть идеи?
РЕДАКТИРОВАТЬ: (дальнейшее объяснение, ошибка noob) Фильтр просматривает слова в поле сообщения и блокирует отправку почты, если слово присутствует.
В настоящее время у меня почти нет спама благодаря recaptcha, но есть как 10 писем в
день рекламы преднизона или виагры. поэтому я бы хотел отфильтровать эти слова, чтобы не получать этот спам
Я пытался вставить упомянутое ранее решение, но я все равно получаю эти письма
Вот мой код
Я попытался поместить код из предыдущего поста в нескольких разных местах без удачи.
Заранее спасибо.
<?php
// require ReCaptcha class
require '../recaptcha-master/src/autoload.php';
//require($_SERVER['DOCUMENT_ROOT'].'/recaptcha-master/src/autoload.php');
// configure
// an email address that will be in the From field of the email.
$from = 'Formulario de contacto <blehblehbleh@somethingsomething.com>';
// an email address that will receive the email with the output of the form
$sendTo = 'InfoPapelCosido <blablabla@somethingsomething.com>';
// subject of the email
$subject = 'Mensaje Nuevo desde la web de PapelCosido';
// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message');
// message that will be displayed when everything is OK :)
$okMessage = 'Mensaje enviado, a la brevedad nos comunicaremos con usted.';
// If something goes wrong, we will display this message.
$errorMessage = 'Hubo un error al enviar el mensaje, por favor intente mas tarde';
// ReCaptch Secret
$recaptchaSecret = '#######################';
// let's do the sending
// if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);
try {
if (!empty($_POST)) {
// validate the ReCaptcha, if something is wrong, we throw an Exception,
// i.e. code stops executing and goes to catch() block
if (!isset($_POST['g-recaptcha-response'])) {
throw new \Exception('ReCaptcha is not set.');
}
// do not forget to enter your secret key from https://www.google.com/recaptcha/admin
$recaptcha = new \ReCaptcha\ReCaptcha($recaptchaSecret, new \ReCaptcha\RequestMethod\CurlPost());
// we validate the ReCaptcha field together with the user's IP address
$response = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if (!$response->isSuccess()) {
throw new \Exception('ReCaptcha was not validated.');
}
// everything went well, we can compose the message, as usually
$emailText = "Mensaje desde la web\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";
}
}
// All the neccessary headers for the email.
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
// THIS IS THE CODE FROM THE POST I'VE CITED BEFORE
//TEST SPAM FILTER
// Prevent spammers from using contact form
//Create an array containing the words in the message
$MessageArray = explode(" ", $emailText );
//Get SPAM words from file and store them in an array
$SpamWords = file_get_contents('spamwords.txt');
$SpamArray = explode("\r\n", $SpamWords);
//Cycle through all the words in the message
foreach($MessageArray as $word){
//Check the word for SPAM words, if it is don't send the email
if(in_array($word, $SpamArray)){
echo '<h1>Spam Guard</h1>';
echo '<p>Here in European Community, the <a href="http://www.legislation.gov.uk/uksi/2003/2426/pdfs/uksi_20032426_en.pdf">Privacy and Electronic Communications Regulations 2003</a> cover the sending of email marketing. This legislation says that organisations must only send marketing emails to anyone if they have agreed to receive them, except where there is a clearly defined customer relationship.</p>';
echo '<p>It appears that you are attempting to send an unsolicited message (e.g. a marketing message).</p>';
echo '<p>We as an organisation do not send unsolicited messages and we request that you do the same for us.</p>';
echo '<p>If you are not attempting to send an unsolicited message, there may be an error in the system so please accept our apologies.</p>';
die();
}
}
//If we've made it to this point, our message doesn't contain any obvious SPAM words
//END SPAM FILTER
// Send email
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'];
}