שימוש ב-reCAPTCHA עם FormMail

חשוב: גרסה 1.0 של reCAPTCHA API לא נתמכת יותר, צריך לשדרג לגרסה 2.0. מידע נוסף

במאמר זה נסביר איך להוסיף reCAPTCHA לסקריפט של FormMail בלי להשתמש במודול 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 במפתח הציבורי שקיבלתם בתהליך יצירת החשבון. היזהרו שלא להשתמש במפתח הפרטי בטעות.

הפעולה הזו תגרום להוספה של שני פרמטרים, שמועברים אל formmail.cgi (או FormMail.pl) באמצעות בקשת POST, כלומר:

  • recaptcha_challenge_field: זה האתגר שנוצר באמצעות המפתח הציבורי.
  • recaptcha_response_field: זו התשובה ששלח המשתמש לאתגר שלמעלה.
  • בצד השרת (איך לבדוק אם המשתמש הזין את התשובה הנכונה)

    בשלב הבא צריך לשנות את formmail.cgi (או FormMail.pl) כדי לטפל בשני הפרמטרים ולאמת את האתגר משרתי ה-reCAPTCHA. בשלב זה, מומלץ ליצור עותק לגיבוי של FormsMail.pl, למקרה הצורך. בקוד שלמטה, הסימן '+' פירושו שצריך להוסיף את השורה לסקריפט של FormMail, וסימן '-' פירושו שצריך להסיר ממנו את השורה. בכל מקרה, אנחנו מראים איפה צריך להוסיף או להסיר את השורות על ידי הצגת השורות הצמודות בסקריפט של 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.)

    לאחר מכן, מוסיפים קוד כדי לקרוא לפונקציונליות של בדיקת 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 אמור לפעול עכשיו באתר שלך.

    קריאה נוספת

  • התאמה אישית של המראה והתחושה
  • טיפים והנחיות
  • פתרון בעיות