【问题标题】:How do I add a verification question to prevent spam?如何添加验证问题以防止垃圾邮件?
【发布时间】: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


【解决方案1】:

添加此检查非常简单。在您的表单 html 中,添加一个新字段:

<p>Zebras are black and...: <input name="animal" type="text" /></p>

然后,在您的 PHP 中,您可以检查它的存在,如果它有问题,请尽早放弃。将此添加到 PHP 文件的顶部:

if (empty($_POST['animal']) || $_POST['url'] !== 'white') {
    http_response_code(400);
    die('wrong answer');
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多