Install reCAPTCHA image verification for PHP

The reCAPTCHA PHP Library provides a simple way to place a CAPTCHA image verification on your PHP website, helping you stop bots from abusing it. The library wraps the reCAPTCHA API.

  1. Download the reCAPTCHA Library, unzip it, and copy recaptchalib.php to the directory where your forms live.
  2. sign up for an API key.
  3. Add code to display the CAPTCHA:
    require_once('recaptchalib.php');
    $publickey = "..."; // you got this from the signup page
    echo recaptcha_get_html($publickey);

  4. In the code that processes the form submission, you need to add code to validate the CAPTCHA. Otherwise, the CAPTCHA will appear, but the answers won’t be checked. The validation code looks like:
    require_once('recaptchalib.php');
    $privatekey = "...";
    $resp = recaptcha_check_answer ($privatekey,
    $_SERVER["REMOTE_ADDR"],
    $_POST["recaptcha_challenge_field"],
    $_POST["recaptcha_response_field"]);

    if (!$resp->is_valid) {
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
    "(reCAPTCHA said: " . $resp->error . ")");
    }

This tip is when you use ssl (https). reCAPTCHA code by default is not for ssl and hence IE8 would show the following popup:-

image

Now if you click Yes – you will not get the reCAPTCHA code since it is http code and if you click No – you will see reCAPTCHA.

Code for reCAPTCHA looks like this :-

require_once('recaptchalib.php');
$publickey = "..."; // you got this from the signup page
echo recaptcha_get_html($publickey);

The reCAPTCHA website does not have any code to explain this. But if you go through the library you will see that you have to change the last line of the above code to say:-

echo recaptcha_get_html($publickey,null,true);

First parameter is your public key, second is your error string and third is whether ssl is enabled. We have used reCAPTCHA for our Asha for Education donation page

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s