【问题标题】:PHP Form Spam FilterPHP 表单垃圾邮件过滤器
【发布时间】:2014-04-15 16:49:02
【问题描述】:

所以我正在尝试学习 PHP,但我在某些代码方面遇到了问题。

我想要实现的是我的网站上有一个非常简单的评论表单,访问者可以使用它向我发送电子邮件,但我收到了很多垃圾邮件。

所以我想做的是放在一个隐藏的空字段中,然后只在它为空时发送电子邮件。如果有人有建议,他们将不胜感激。

这是我的 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>

还有我的 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"); 
}

?>

我昨天一整天都尝试了几种代码变体,但似乎无法使其正常工作。感谢大家的建议,希望今天能学到一些东西。

【问题讨论】:

  • 表单中的隐藏字段在哪里?
  • 无论如何,您认为这会有什么帮助?无论您需要隐藏字段包含什么,垃圾邮件发送者都可以将相同的内容放入其中。
  • 如果您想减缓自动垃圾邮件发送者的速度,您需要使用 CAPTCHA 之类的东西。
  • 一旦你弄清楚如何减缓自动垃圾邮件发送者的速度(我们大多数人都使用验证码或 csrf 令牌),你应该弄清楚如何防止标题注入到你的电子邮件中......这要糟糕得多有问题。
  • 很多人使用这个系统说垃圾邮件机器人倾向于填写表格中的所有字段。因此,如果您将其设置为仅在为空时发送,它们就可以工作。

标签: php forms filter


【解决方案1】:

首先将空字段添加到您的表单中。

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

将其添加到您的 CSS 文件中以向真实的人隐藏它:

#phone_number { display: none; }

检查该字段是否有检测垃圾邮件的内容:

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

【讨论】:

    【解决方案2】:

    当垃圾邮件机器人第一次读取您的网站时,它实际上会抓取并存储您的字段名称和表单的 action="send.php" 属性,然后每隔一段时间,它会将数据直接卷曲到您的 send.php文件,因此它完全忽略了表单的新内容。

    因此,为了立即缓解,您实际上希望这样做:

    html 页面

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

    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"); 
        }
    }
    

    【讨论】:

      【解决方案3】:

      此页面包含许多垃圾邮件发送者帐户:http://www.stopforumspam.com/您可以将文件下载为 CSV 文件并使用 php 阻止它们。

      最简单的必须是设置验证码。下面是 PHP 创建验证码图片的教程:THENEWBOSTON

      如果您认为垃圾邮件机器人会在所有表单数据中填充一些信息。基本上按照你说的创建一个隐藏字段。

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

      到你的 php 代码:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-17
        • 2012-01-15
        • 1970-01-01
        • 2010-12-31
        • 2011-12-22
        • 2019-09-19
        • 2012-10-16
        • 1970-01-01
        相关资源
        最近更新 更多