ITEEDU

47.17. Zend_Service_ReCaptcha

47.17.1. Introduction

Zend_Service_ReCaptcha provides a client for the reCAPTCHA Web Service. Per the reCAPTCHA site, "reCAPTCHA is a free CAPTCHA service that helps to digitize books." Each reCAPTCHA requires the user to input two words, the first of which is the actual captcha, and the second of which is a word from some scanned text that Optical Character Recognition (OCR) software has been unable to identify. The assumption is that if a user correctly provides the first word, the second is likely correctly entered as well, and can be used to improve OCR software for digitizing books.

In order to use the reCAPTCHA service, you will need to sign up for an account and register one or more domains with the service in order to generate public and private keys.

47.17.2. Simplest use

Instantiate a Zend_Service_ReCaptcha object, passing it your public and private keys:

$recaptcha = new Zend_Service_ReCaptcha($pubKey, $privKey);

To render the reCAPTCHA, simply call the getHTML() method:

echo $recaptcha->getHTML();

When the form is submitted, you should receive two fields, 'recaptcha_challenge_field' and 'recaptcha_response_field'. Pass these to the ReCaptcha object's verify() method:

$result = $recaptcha->verify(
    $_POST['recaptcha_challenge_field'],
    $_POST['recaptcha_response_field']
);

Once you have the result, test against it to see if it is valid. The result is a Zend_Service_ReCaptcha_Response object, which provides an isValid() method.

if (!$result->isValid()) {
    // Failed validation
}

Even simpler is to use the ReCaptcha Zend_Captcha adapter, or to use that adapter as a backend for the Captcha form element. In each case, the details of rendering and validating the reCAPTCHA are automated for you.