首页 文章

防止php web联系表单垃圾邮件

提问于
浏览
5

我是一名业余网页设计师,我在stackoverflow.com和其他网站上搜索过并找到了很多针对此问题的修复程序,但是没有一个有效(可能是因为我错误地实现了它们) . 我希望有更多知识的人可以帮我解决一个简单的问题,或者告诉我如何实现我发现的一个修复程序 .

问题:我在我的公司网站上有一个非常简单的php联系表格 . 它已经运作了很多年,但在上周被黑了 . 我现在每天收到数百份没有评论的联系表格提交,他们只有(显然是有效的)电子邮件地址,以及名称字段中的一串字符(如“58ee8b52eef46”) .

我已经尝试了几种技术来防止这种垃圾邮件,它们要么破坏我的PHP表单,要么它们不会阻止垃圾邮件 . If possible I would like a solution that does NOT require a Captcha distorted text test, and does NOT require all fields of the form to be filled.

这是我的完整PHP代码:

<?php
if(isset($_POST['email'])) {
$email_to = "myemail@example.com";
$email_subject = "website form submission";

function died($error) {
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.

"; echo $error."

"; echo "Please go back and fix these errors.

"; die(); } if(!isset($_POST['name']) || !isset($_POST['email']) || !isset($_POST['telephone']) || !isset($_POST['comments'])) { died('We are sorry, but there appears to be a problem with the form you submitted.'); } $name = $_POST['name']; $email_from = $_POST['email']; $telephone = $_POST['telephone']; $comments = $_POST['comments']; $error_message = ""; if(strlen($error_message) > 0) { died($error_message); } $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "Name: ".clean_string($name)."\n"; $email_message .= "Email: ".clean_string($email_from)."\n"; $email_message .= "Telephone: ".clean_string($telephone)."\n"; $email_message .= "Comments: ".clean_string($comments)."\n"; $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); ?> Thank you for contacting us. We will be in touch with you soon. You will now be redirected back to example.com. <META http-equiv="refresh" content="2;URL=http://www.example.com"> <?php } die(); ?>

4 回答

  • 0

    一个简单的技巧是创建一个蜜 jar 字段:

    HTML

    <!-- within your existing form add this field -->
    <input type="text" id="website" name="website"/>
    

    CSS

    /*in your css hide the field so real users cant fill it in*/
    form #website{ display:none; }
    

    PHP

    //in your php ignore any submissions that inlcude this field
    if(!empty($_POST['website'])) die();
    
  • 0

    创建一个表单域并为用户隐藏它 . 在php脚本中检查是否提交了此字段,但是为空 . 现在您知道请求来自您的表单和用户 .

    垃圾邮件将填充隐藏字段,或者如果他们使用您的PHP脚本直接垃圾邮件保护字段未设置 .

    HTML

    <input name="website" type="text" class="website"/>
    

    CSS

    form .website{ display:none; } /* hide because is spam protection */
    

    PHP

    # spam protection
    if (isset($_POST["website"]) && $_POST["website"] == "") {
      # your php code to mail here
    } else {
      http_response_code(400);
      exit;
    }
    

    你可以在这里找到更多方法来保护php表单垃圾邮件:https://zinoui.com/blog/protect-web-forms-from-spam

  • 1

    如果您收到的垃圾邮件没有评论,为什么不简单地添加一个支票呢?真正的人类访问者没有理由在没有评论的情况下提交您的联系表格 .

    由于您不想添加验证码,因此短期内最简单的解决方案是检查评论是否为最小字符数且至少包含一定数量的单词 .

    例如:

    $comments = trim($_POST['comments']); // trim() to strip off whitespace from beginning and end, like spaces and linebreaks
    
    if (strlen($comments) < 20 || substr_count($comments, " ") < 3) {
        died('Your comment is too short.');
    }
    

    这是一个非常简单的检查,以查看注释包含至少20个字符和至少3个空格(4个单词) . 根据需要调整 .

  • 21

    隐藏的字段,愚蠢的问题(什么是3 4?)等在阻止表单上的垃圾邮件方面不是很有效 .

    几年前我研究了这个,并提出了一个我称之为“FormSpammerTrap”的解决方案 . 它使用JavaScript代码“监视”焦点/ onclick所需的字段 . 自动化流程,除非针对特定站点进行高度自定义(比spambot所有者想要花费更多时间),否则无法“关注/点击”必填字段 .

    我的www.FormSpammerTrap.com网站上有免费的解决方案 . 并且有超过3年的时间's a form there that spambots can try to spam...and they haven't . 欢迎您试一试......收获您的电子邮件.'s all open source, so you can see how it works. (And, if you use the form, I don'收获您的电子邮件 . 我回复一次,然后删除你的电子邮件 . )

    我的技术在阻止垃圾邮件时更有效 . 他们无法在该网站上设置联系表格 .

相关问题