חשוב: אין יותר תמיכה בגרסה 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, כלומר:
בצד השרת (איך בודקים אם המשתמש הזין את התשובה הנכונה)
בשלב הבא, עליך לשנות את 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; +
(לשם כך יש צורך במודול 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 אמור לפעול עכשיו באתר שלך.