我怎样才能改善自己的形式?

时间:2012-09-14 06:04:44

标签: php forms

目前,我们收到大量垃圾邮件。我刚刚添加了以下内容:

if ($field_budget == "" || $field_timeline == "" || $field_email == "" || $field_name == "") { echo "Please fill out all required form details, thank you!"; } else {

这有助于持续数周,但现在垃圾邮件发送者又回来了。什么是一种简单,优雅的方式来改善我的形式(下面的完整代码)?

<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_website = $_POST['cf_website'];
$field_company = $_POST['cf_company'];
$field_budget = $_POST['cf_budget'];
$field_custombudget = $_POST['cf_custombudget'];
$field_timeline = $_POST['cf_timeline'];
$field_message = $_POST['cf_message'];

$mail_to = 'sean@seanduran.com';
$subject = 'Message from a site visitor, '.$field_name;

$body_message = 'Name: '.$field_name."\n";
$body_message .= 'e-mail: '.$field_email."\n";
$body_message .= 'Website: '.$field_website."\n";
$body_message .= 'Company: '.$field_company."\n";
$body_message .= 'Budget: '.$field_budget."\n";
$body_message .= 'Custom Budget: '.$field_custombudget."\n";
$body_message .= 'Timeline: '.$field_timeline."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

if ($field_budget == "" || $field_timeline == "" || $field_email == "" || $field_name == "") {
    echo "Please fill out all required form details, thank you!";
} else {
    $mail_status = mail($mail_to, $subject, $body_message, $headers);

    if ($mail_status) { ?>
        <script language="javascript" type="text/javascript">
            alert('Thank you for the message. We will contact you shortly.');
            window.location = 'http://seanduran.com';
        </script>
    <?php
    }
    else { ?>
        <script language="javascript" type="text/javascript">
            alert('Message failed. Please, send an email to sean@seanduran.com');
            window.location = 'http://seanduran.com';
        </script>
    <?php
    }
}
?>

4 个答案:

答案 0 :(得分:3)

你可以使用一个技巧 -

插入一个文本字段,通过CSS(id)隐藏它并检查它必须是空的!

所有的垃圾邮件都会在字段中添加一些内容,所以只需给它一个名称“地址”或类似内容......

如果机器人填写它,你可以给出错误......

其他提示只是使用ReCaptcha

答案 1 :(得分:1)

我会介绍一个Captcha。垃圾邮件发送者也很擅长解决这些问题,但这是一个很好的额外措施。看看例如reCAPTCHA,添加起来应该不会太难(参见instructions here)。

答案 2 :(得分:0)

进行一般性更改

  • 验证所有帖子数据。检查标题注入
  • 验证码

查看这篇文章是一个好的开始:http://www.digital-web.com/articles/bulletproof_contact_form_with_php/

答案 3 :(得分:0)

将您的姓名和电子邮件字段更改为:

Name: <input type="text" name="cf_123" />
Email: <input type="text" name="cf_456" />

如此引用:

$field_name  = $_POST['cf_123'];
$field_email = $_POST['cf_456'];

然后添加以下字段:

<input type="text" name="name" class="foo" />
<input type="text" name="email" class="foo" />

..用这个CSS:

.foo {
    display : none;
}

然后你可以简单地将这个条件添加到你的PHP:

if (!empty($_POST['name']) || !empty($_POST['email'])) {
    return false; // only a bot would see those fields and fill them out. 
}