สำคัญ: ไม่รองรับ reCAPTCHA API เวอร์ชัน 1.0 อีกต่อไป โปรดอัปเกรดเป็นเวอร์ชัน 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
2 รายการด้วยคีย์สาธารณะที่คุณได้รับระหว่างการสร้างบัญชี โปรดระมัดระวังอย่าใช้งาน
โดยไม่ตั้งใจ
โดยพื้นฐานแล้ว การดำเนินการนี้จะเพิ่มพารามิเตอร์ 2 รายการ ซึ่งส่งไปยัง formmail.cgi (หรือ FormMail.pl) ผ่านคำขอ POST ดังนี้
ฝั่งเซิร์ฟเวอร์ (วิธีทดสอบว่าผู้ใช้ป้อนคำตอบที่ถูกต้องหรือไม่)
จากนั้น คุณต้องแก้ไข formmail.cgi (หรือ FormMail.pl) เพื่อจัดการกับพารามิเตอร์ทั้ง 2 รายการ และเพื่อ ตรวจสอบความถูกต้องของคำขอจากเซิร์ฟเวอร์ 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 ก็ควรทำงานในเว็บไซต์ของคุณแล้ว