将 reCAPTCHA 与 FormMail 搭配使用

重要提示:reCAPTCHA API 1.0 版已不再受支持,请升级到 2.0 版。了解详情

下面,我们将介绍如何在不使用 reCAPTCHA Perl 模块的情况下将 reCAPTCHA 添加到您的 FormMail 脚本。如果您知道自己在做什么,也可以使用 reCAPTCHA Perl 模块。

客户端(如何显示人机识别系统图片)

在 HTML 网页的 <form> 元素内,您必须添加以下代码:

  <script type="text/javascript"
    src="http://www.google.com/recaptcha/api/challenge?k=your_public_key">
  </script>
  <noscript>
    <iframe src="http://www.google.com/recaptcha/api/noscript?k=your_public_key"
        height="300" width="500" frameborder="0"></iframe>

    <textarea name="recaptcha_challenge_field" rows="3" cols="40">
    </textarea>
    <input type="hidden" name="recaptcha_response_field"
        value="manual_challenge">
  </noscript>

这或许不言而喻,但无论如何,我们还是要说:您需要将两个 your_public_key 实例替换为您在帐号创建过程中收到的公钥。请注意不要误用您的私钥。

这基本上会添加两个参数,这些参数将通过 POST 请求传递给 formmail.cgi(或 FormMail.pl),即:

  • recaptcha_challenge_field:这是通过您的公钥创建的验证。
  • recaptcha_response_field:这是用户针对上述验证提交的响应。
  • 服务器端(如何测试用户是否输入了正确答案)

    接下来,您需要修改 formmail.cgi(或 FormMail.pl)以处理这两个参数,并验证来自 reCAPTCHA 服务器的质询。此时,为保险起见,建议您备份 FormMail.pl。在以下代码中,“+”表示需要向 FormMail 脚本添加该行,“-”表示需要从中移除该行。无论是哪种情况,我们都会通过在 FormMail 脚本中显示相邻行来显示需要添加或移除这些行的位置。

    首先,您需要将以下代码行添加到 FormMail 中,告知 Perl 使用模块 LWP::UserAgent:

     # ACCESS CONTROL FIX: Peter D. Thompson Yezek                                #
     #                     http://www.securityfocus.com/archive/1/62033           #
     ##############################################################################
     +use LWP::UserAgent;
     +
    

    (这要求模块 LWP::UserAgent 位于 Perl 环境中。大多数安装的 Perl 均已包含此模块。如果未安装该模块,请参考 有关安装 Perl 模块的基本说明。)

    然后,添加代码以调用下文介绍的人机识别系统检查功能。

     # Check Required Fields
     &check_required;
    
     +# Check the captcha challenge and response.
     +&check_captcha;
     +
     # Send E-Mail
     &send_mail;
    
     # Return HTML Page or Redirect User
     &return_html;
    

    现在,验证人机识别系统响应,如果响应与质询不匹配,则生成错误。

     +##############################################################################
     +# Check the CAPTCHA response via the reCAPTCHA service.
     +sub check_captcha {
     +
     +      my $ua = LWP::UserAgent->new();
     +      my $result=$ua->post(
     +      'https://www.google.com/recaptcha/api/verify',
     +      {
     +          privatekey => 'your_private_key',
     +          remoteip   => $ENV{'REMOTE_ADDR'},
     +          challenge  => $Form{'recaptcha_challenge_field'},
     +          response   => $Form{'recaptcha_response_field'}
     +      });
     +
     +      if ( $result->is_success && $result->content =~ /^true/) {
     +              return;
     +      } else {
     +              &error('captcha_failed');
     +      }
     +}
     +
     # NOTE rev1.91: This function is no longer intended to stop abuse, that      #
     #    functionality is now embedded in the checks made on @recipients and the #
     #    recipient form field.                                                   #
    

    最后,创建用于在检查失败时输出错误消息的功能:

             if ($Config{'missing_fields_redirect'}) {
                 print "Location: " . &clean_html($Config{'missing_fields_redirect'}) . "\n\n";
             }
     +    }
     +    elsif ($error eq 'captcha_failed') {
     +            print <<"(END ERROR HTML)";
     +Content-type: text/html
     +
     +<html>
     + <head>
     +  <title>Error: Captcha Check Failed</title>
     + </head>
     + <body bgcolor=#FFFFFF text=#000000>
     + <center>
     +  <table border=0 width=600 bgcolor=#9C9C9C>
     +    <tr><th><font size=+2>Error: Captcha Check Failed</font></th></tr%gt;
     +   </table>
     +  <table border=0 width=600 bgcolor=#CFCFCF>
     +    <tr><td>The Captcha response of the form you submitted did not match the challenge.
     +     Please check the form and make sure that your response matches the challenge in the captcha image.
     +     You can use the browser back button to return to the form.
     +     </center%gt;
     +    </td></tr>
     +   </table>
     +  </center>
     + </body>
     +</html>
     +(END ERROR HTML)
     +    }
             else {
                  foreach $missing_field (@error_fields) {
                      $missing_field_list .= "<li>" . &clean_html($missing_field) . "\n";
     .
     .
     .
      </html>
     (END ERROR HTML)
             }
     -    }
     -
         exit;
     }
    

    大功告成!现在,reCAPTCHA 应该可以在您的网站上运行。

    延伸阅读

  • 自定义外观和风格
  • 提示和指南
  • 问题排查