Zend_Service_ReCaptchaIntroductionZend_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.
Simplest use
Instantiate a Zend_Service_ReCaptcha object, passing it
your public and private keys:
Creating an instance of the reCAPTCHA service
To render the reCAPTCHA, simply call the getHTML()
method:
Displaying the reCAPTCHAgetHTML();
]]>
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:
Verifying the form fieldsverify(
$_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.
Validating the reCAPTCHAisValid()) {
// 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.
Hiding email addressesZend_Service_ReCaptcha_MailHide can be used to hide email
addresses. It will replace a part of an email address with a link that opens a popup
window with a reCAPTCHA challenge. Solving the challenge will reveal the complete
email address.
In order to use this component you will need
an account to generate
public and private keys for the mailhide API.
Using the mail hide componentsetPublicKey($pubKey);
$mailHide->setPrivateKey($privKey);
$mailHide->setEmail($mail);
// Display it
print($mailHide);
]]>
The example above will display "m...@example.com" where "..." has a link that opens up
a popup window with a reCAPTCHA challenge.
The public key, private key, and the email address can also be specified in the
constructor of the class. A fourth argument also exists that enables you to set some
options for the component. The available options are listed in the following table:
Zend_Service_ReCaptcha_MailHide optionsOptionDescriptionExpected ValuesDefault ValuelinkTitleThe title attribute of the linkstring'Reveal this e-mail address'linkHiddenTextThe text that includes the popup linkstring'...'popupWidthThe width of the popup windowint500popupHeightThe height of the popup windowint300
The configuration options can be set by sending them as the fourth argument to the
constructor or by calling setOptions($options), which takes
an associative array or an instance of Zend_Config.
Generating many hidden email addressessetPublicKey($pubKey);
$mailHide->setPrivateKey($privKey);
$mailHide->setOptions(array(
'linkTitle' => 'Click me',
'linkHiddenText' => '+++++',
));
// The mail addresses we want to hide
$mailAddresses = array(
'mail@example.com',
'johndoe@example.com',
'janedoe@example.com',
);
foreach ($mailAddresses as $mail) {
$mailHide->setEmail($mail);
print($mailHide);
}
]]>