將 reCAPTCHA 與表單 Mail 搭配使用

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

以下說明如何在不使用 reCAPTCHA Perl 模組。掌握這類動態時,可以改用 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 (或 FormsMail.pl) 來處理這兩個參數,並將 透過 reCAPTCHA 伺服器驗證驗證問題。在此時刻,建議您為 FormMail.pl 建立備份副本,以防萬一。在下方程式碼中,「+」意味著 必須加到 FormMail 指令碼中,並以「-」代表您需要移除該線條 在每個案例中,我們都會在檔案旁顯示需要加入或移除線條的位置 。

    首先,您必須新增下列程式碼,指示 Perl 使用模組 LWP::UserAgent 至 formMail:

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

    現在,驗證人機驗證 (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 現在應該可以開始使用你的網站了。

    其他資訊

  • 自訂外觀和風格
  • 提示和規範
  • 疑難排解