November 29th, 2010 · php zend framework 1

Using Zend_Captcha_ReCaptcha

Short introduction to using Zend_Captcha_ReCaptcha as a standalone entity. The Zend Framework manual does not cover the use of this component well; I am working on writing a more detailed manual entry for it, of which this is the first step.

This tutorial is aimed at developers who are introducing Zend Framework (ZF) components into existing non-ZF web sites (like me!), however it can easily be adapted for those who are creating new web sites based on ZF (ie: using Zend_Application).

Step 1: Create Service Object

Zend_Captcha_ReCaptcha communicates with Google's reCAPTCHA service via the Zend_Service_ReCaptcha class:

$recaptcha_service = new Zend_Service_ReCaptcha(

Where $rcPublicKey and $rcPrivateKey are your public and private keys, respectively.

Short introduction to using Zend_Captcha_ReCaptcha.

Step 2: Create instance of CAPTCHA

$adapter = new Zend_Captcha_ReCaptcha();
$adapter->setService( $recaptcha_service );

There are additional options you can provide to $adapter, all of which are documented in the API guide. If you are using Zend_Controller and Zend_View, this is the object you would assign to your view.

Step 3: Displaying the CAPTCHA

In the HTML page for your form, call the render function of $adapter to display the CAPTCHA:

echo $adapter->render();

Step 4: CAPTCHA Validation

In the PHP code which validates the form to which you added the CAPTCHA in Step 3, add the following to your validation routine:

$reUserData = array(
    'recaptcha_challenge_field' => $_POST['recaptcha_challenge_field'],
    'recaptcha_response_field' => $_POST['recaptcha_response_field']
if ( $adapter->isValid($reUserData) !== true )
    // Handle validation error

The important part to note here is that isValid() requires an associative array of the two reCAPTCHA form inputs, which neither the Zend Framework manual nor API guide stipulate.