Add reCAPTCHA to a form

You may have seen those funny squigly words on contact forms that you have to fill out before you can submit them. Those in general are refered to as reCAPTCHA fields and they're easy to add, once you know what to do and how they work. For a quick introduction please look here.

1) Get your private/public key pair:

Go to the signup page and get your public and private keys!

2) Add this javascript snippet to the page the form will be on:

<script type="text/javascript" src="https://www.google.com/recaptcha/api/challenge?k=YOUR_PUBLIC_KEY" </script> <noscript> <iframe src="https://www.google.com/recaptcha/api/noscript?k=YOUR_PUBLIC_KEY" height="300" width="500" frameborder="0"></iframe><br> </noscript> *fill in YOUR_PUBLIC_KEY with the one from the signup process

3) Include the recaptchalib.php in the page where the user will enter their guess.

 <form method="post" action="verify.php">
   <?php
     require_once('recaptchalib.php');
     $publickey = "YOUR_PUBLIC_KEY"; // you got this from the signup page
     echo recaptcha_get_html($publickey);
   ?>
   <input type="submit" />
 </form><br>
		
*fill in YOUR_PUBLIC_KEY with the one from the signup process
		

4) Add the code to check if it's correct in the form that's submitted before doing whatever else with it. Using the example above we would call this php file verify.php

			
<?php
 require_once('recaptchalib.php');
 $privatekey = "YOUR_PRIVATE_KEY";
 $resp = recaptcha_check_answer ($privatekey,
                                 $_SERVER["REMOTE_ADDR"],
                                 $_POST["recaptcha_challenge_field"],
                                 $_POST["recaptcha_response_field"]);
 if (!$resp->is_valid) {
   // What happens when the CAPTCHA was entered incorrectly
   die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
        "(reCAPTCHA said: " . $resp->error . ")");
 } else {
   // Your code here to handle a successful verification
 }
 ?>
*fill in YOUR_PRIVATE_KEY with the one from the signup process