PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Simplest use

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

Example 839. Creating an instance of the reCAPTCHA service

<?php
$recaptcha 
= new Zend_Service_ReCaptcha($pubKey$privKey);

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

Example 840. Displaying the reCAPTCHA

<?php
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:

Example 841. Verifying the form fields

<?php
$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.

Example 842. Validating the reCAPTCHA

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

It is even simpler 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.

Zend Framework