Answer the question
In order to leave comments, you need to log in
Adding a check for stop words to a contact form?
Dear experts.
I have such a feedback form on the site, I tried to supplement it with a filter of bad words. If they are found, it is supposed to display a message and list the found words.
<?php
require_once(rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/wp-load.php');
if(isset($_POST['submit'])):
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
$secret = 'yyyyy';
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
$name = !empty($_POST['name'])?$_POST['name']:'';
$email = !empty($_POST['email'])?$_POST['email']:'';
$message = !empty($_POST['message'])?$_POST['message']:'';
$useragent = $_SERVER['HTTP_USER_AGENT'];
$comm_ip = get_ipaddress();
if($responseData->success):
$badWords = array("плохое_слово1","плохое_слово2", "плохое_слово3");
$matches = array();
$matchFound = preg_match_all(
"/\b(" . implode($badWords,"|") . ")\b/i",
$message,
$matches
);
if ($matchFound) {
$words = array_unique($matches[0]);
foreach($words as $word) {
echo "<li>" . $word . "</li>";
}
echo "</ul>";
}
$to = '[email protected]';
$subject = 'Сообщение';
$htmlContent = "
<h1>Данные отправителя:</h1>
<p><b>Имя: </b>".$name."</p>
<p><b>E-mail: </b>".$email."</p>
<p><b>Сообщение: </b>".$message."</p>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From:'.$name.' <'.$email.'>' . "\r\n";
wp_mail($to,$subject,$htmlContent,$headers);
$succMsg = 'Ваше сообщение успешно отправлено';
$name = '';
$email = '';
$message = '';
else:
$errMsg = 'Ошибка проверки «Я не робот», попробуйте снова.';
endif;
else:
$errMsg = 'Вы забыли поставить галочку, что вы не робот. Сообщение не было отправлено.';
endif;
else:
$errMsg = '';
$succMsg = '';
$name = '';
$email = '';
$message = '';
endif;
?>
<html>
<head>
</head>
<body>
<h2>Контактная форма</h2>
<?php if(!empty($errMsg)): ?><div class="errMsg"><?php echo $errMsg; ?></div><?php endif; ?>
<?php if(!empty($succMsg)): ?><div class="succMsg"><?php echo $succMsg; ?></div><?php endif; ?>
<form action="" method="POST">
<input type="text" class="text" required="" value="<?php echo !empty($name)?$name:''; ?>" placeholder="Введите ваше имя" name="name" >
<input type="email" class="text" required="" value="<?php echo !empty($email)?$email:''; ?>" placeholder="Укажите ваш E-mail адрес" name="email" >
<textarea type="text" placeholder="Сообщение..." required="" name="message"><?php echo !empty($message)?$message:''; ?></textarea>
<div class="g-recaptcha" data-sitekey="xxx"></div>
<input type="submit" name="submit" class="submit" value="ОТПРАВИТЬ">
</form>
</body>
</html>
$badWords = array("плохое_слово1","плохое_слово2");
$matches = array();
$matchFound = preg_match_all(
"/\b(" . implode($badWords,"|") . ")\b/i",
$message,
$matches
);
if ($matchFound) {
$words = array_unique($matches[0]);
foreach($words as $word) {
echo "<li>" . $word . "</li>";
}
echo "</ul>";
}
Answer the question
In order to leave comments, you need to log in
<?php
$message = "плохое_слово1 xорошее
плохое_слово2";
$badWords = array("плохое_слово1","плохое_слово2");
$matches = array();
$matchFound = preg_match_all(
"/(" . implode($badWords,"|") . ")/mui",
$message,
$matches
);
if ($matchFound) {
$words = array_unique($matches[0]);
foreach($words as $word) {
echo "<li>" . $word . "</li>";
}
echo "</ul>";
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question