【发布时间】:2019-12-06 07:16:10
【问题描述】:
我有一个简单的联系表格。效果很好 - 除了我每天收到的几封电子邮件。垃圾邮件!!我想添加一个简单的验证问题(例如“斑马是黑色的......”),其中只接受某个答案,否则不会发送表格。我该怎么办?
反垃圾邮件根本不起作用!
HTML:
<form action="submit.php" class="col4" method="post">
<p>Your name: <input name="name" type="text" size="100" />
</p>
<p>Your email: <input name="email" type="text" size="100" /> </p>
<p><input name="website" type="text" class="website"/></p>
<p class="antispam">Leave this empty: <input type="text" name="url" />
</p>
<p>Message: <textarea name="message" cols="100"></textarea>
</p>
<p><input type="submit" value="Send" /></p>
</form>
CSS:
form .website{ display:none; } /* hide because is spam protection */
PHP:
<?php
# spam protection
if (isset($_POST["website"]) && $_POST["website"] == "") {
# your php code to mail here
} else {
http_response_code(400);
exit;
}
// if the url field is empty
if (isset($_POST['url']) && $_POST['url'] == '') {
// put your email address here
$youremail = 'admin@napleswebgraphics.com';
// prepare a "pretty" version of the message
$body = "This is the form that was just submitted<br />
:
Name: $_POST[name]
E-Mail: $_POST[email]
Message: $_POST[message]";
// Use the submitters email if they supplied one
// (and it isn't trying to hack your form).
// Otherwise send from your email address.
if ($_POST['email'] && !preg_match("/[\r\n]/", $_POST['email'])) {
$headers = "From: $_POST[email]";
} else {
$headers = "From: $youremail";
}
// finally, send the message
mail($youremail, 'Contact Form', $body, $headers);
} // otherwise, let the spammer think that they got their message through ?>
<h1 align="center">Thank You!</h1>
<div align="center">I'll get back to you as soon as possible! Click <a
href="index.html">here</a> to go back to the main page.
</div>
我希望一个特定的答案是唯一允许表单通过的东西。
【问题讨论】:
-
验证码是“黄金”标准。
标签: php html contact-form