简体   繁体   English

PHP表格垃圾邮件过滤器

[英]PHP Form Spam Filter

So I'm trying to learn PHP but I'm have trouble with a certain code. 因此,我尝试学习PHP,但是在使用某些代码时遇到了麻烦。

What I am trying to achieve is I have a very simple comment form on my site, visitor can use it to send me an email, but I am getting a lot of spam. 我要实现的目标是,我的网站上有一个非常简单的评论表单,访问者可以使用它向我发送电子邮件,但是我收到了大量垃圾邮件。

So I am trying to do is put in a hidden empty field then only send the email if it is empty. 所以我想做的是将其放在一个隐藏的空字段中,然后仅在电子邮件为空时发送它。 If any one have suggestions they would be much appreciated. 如果有人提出建议,将不胜感激。

Here is my HTML code: 这是我的HTML代码:

<form id="contact" name="contact" method="post" action="send.php">
   <h3 class="send-comments-hdr">Your Comments?</h3>
   <ul class="form-holder">
      <li><label for="name" class="comment-labels">Name</label></li>
      <li><input type="text" name="name" id="name" class="comment-inputs" placeholder="Your Name"/></li>
      <li><label for="email-input" class="comment-labels">Email</label></li>
      <li><input type="email" name="email" id="email-input" class="comment-inputs" placeholder="your.name@example.com" /></li>
      <li><label for="comments" class="comment-labels">Comments</label></li>
      <li><textarea name="comments" id="comments" class="comment-inputs" rows="4"></textarea></li>
      <li class="center"><button id="send-comments" type="submit" class="send-button">Send</button></li>
   </ul>
</form>

And my PHP sending file: 和我的PHP发送文件:

<?
$parent = $_SERVER["HTTP_REFERER"];

$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];

$to = "myemail@gmail.com";
$subject = "Comments From a Website Visitor";
$message = "<p>A visitor from mydomain.com has left the following comments:</p>";
$message .= '<html><body>';
$message .= '<table rules="all" style="border-color: #666" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . $name . "</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>" . $email . "</td></tr>";
$message .= "<tr><td><strong>Comments:</strong> </td><td>" . $comments . "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
$message .= "<p>Please reply ASAP, Thank you.</p>";

$headers = "From: $name <$email>" . "\r\n";
$headers .= "Reply-To: $email" . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

if (mail($to, $subject, $message, $headers)) {
    header("Location:$parent"); 
}

?>

I tried all day yesterday several code variations but could not seem to get it to work. 我昨天整天尝试了几种代码变体,但似乎无法使其正常工作。 Thank for all the advice, hoping to learn something today. 感谢您的所有建议,希望今天能学到一些东西。

First add the empty field to your form. 首先将空字段添加到您的窗体。

<form id="contact" name="contact" method="post" action="send.php">
<input type="text" name="phone_number" id="phone_number">

Add to your CSS files to hide it from actual people: 添加到您的CSS文件中,以使其对实际人员隐藏:

#phone_number { display: none; }

Check if the field has something to detect spam: 检查该字段是否有检测垃圾邮件的内容:

if ($_POST['phone_number']) {
    echo "Thank you for your lovely spam.";
    exit();
}

When a spam bot reads your site for the first time it will actually scrape up and store your field names and the action="send.php" attribute of your form and then on an interval it will simply cURL the data directly to your send.php file so it completely ignores your form's new content. 当垃圾邮件机器人第一次读取您的网站时,它实际上会抓取并存储您的字段名称和表单的action="send.php"属性,然后每隔一段时间,它就会将数据直接卷曲到您的send.php action="send.php" send.php文件,因此它完全忽略了表单的新内容。

So for immediate relief you would actually want to do this: 因此,为了立即获得救济,您实际上想这样做:

html page HTML页面

<input type="hidden" name="HIDDEN_TRAP" value="" />
<!-- ^ Add this line inside of your <form> -->

send.php send.php

// HIDDEN_TRAP will be missing completely when the spam bot sends again
if(isset($_POST['HIDDEN_TRAP'])) {
    if (mail($to, $subject, $message, $headers)) {
        header("Location:$parent"); 
    }
}

This page contains many spammer accounts: http://www.stopforumspam.com/ You can download the file as a CSV file and block them with php. 该页面包含许多垃圾邮件发送者帐户: http : //www.stopforumspam.com/您可以将文件下载为CSV文件,并使用php阻止它们。

And the easiest must be setting a capthca. 最简单的方法是设置capthca。 Here is a tutorial for Creating a Captcha image in PHP: THENEWBOSTON 这是在PHP中创建验证码图像的教程: THENEWBOSTON

If you think that spam bots fill all the form data with some information. 如果您认为垃圾邮件漫游器会用一些信息填充所有表单数据。 Basicly create a hidden field as you told. 基本上按照您的说明创建一个隐藏字段。

<input type="hidden" name="antibot" value="" />

and to your php code: 和你的PHP代码:

if ( $_POST['antibot'] !== "" ) {
throw new Exception ("Bot detected.");
}else {
//--- THE REST OF YOUR CODE
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM