將 reCAPTCHA 與表單 Mail 搭配使用

重要事項:系統已不再支援 reCAPTCHA API 1.0 版,請升級至 2.0 版。瞭解詳情

我們將說明如何在不使用 reCAPTCHA Perl 模組的情況下,將 reCAPTCHA 新增至 CardMail 指令碼。如果您知道自己的工作為何,可以選擇使用 reCAPTCHA Perl 模組。

用戶端 (如何顯示人機驗證 (Captcha) 圖片)

在 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 指令碼中顯示鄰近行,以顯示應新增或移除行的位置。

    首先,您需要指示 Perl 使用 LWP::UserAgent 模組,方法是在 FormMail 中新增下列程式碼:

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

    (您必須在 Perl 環境中採用 LWP::UserAgent 模組。大部分的 Perl 安裝項目都已有這個模組。如果未安裝模組,請參閱 安裝 Perl 模組的一些基本操作說明。)

    然後,新增程式碼來呼叫以下定義的人機驗證 (Captcha) 功能。

     # 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;
    

    現在,請驗證人機驗證 (Captcha) 回應,並在回應與驗證條件不符時產生錯誤。

     +##############################################################################
     +# 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 現在應該能在網站上正常運作。

    延伸閱讀

  • 自訂外觀和風格
  • 提示和指南
  • 疑難排解