Zend_Service_ReCaptchaIntroductionZend_Service_ReCaptcha fournit un client pour le Service Web reCAPTCHA. D'après le site de reCAPTCHA,
"reCAPTCHA est un service gratuit de CAPTCHA qui aide à la numérisation de livres."
Chaque reCAPTCHA requière que l'utilisateur saisisse 2 mots, le premier est le CAPTCHA,
et le second est issu de texte scanné que les OCR (Optical Character Recognition) ne
peuvent identifier.
Pour utiliser le service reCAPTCHA, vous devez créer un compte et enregistrer un
ou plusieurs domaines d'utilisation afin de générer une clé publique et une
privée.
Utilisation la plus simple
Instanciez un objet Zend_Service_ReCaptcha en lui passant
vos clés publique et privée :
Créer une instance de service ReCaptcha
Pour rendre le reCAPTCHA, appelez simplement la méthode getHTML()
:
Afficher le ReCaptchagetHTML();
]]>
Lorsque le formulaire est envoyé, vous devriez recevoir 2 champs
'recaptcha_challenge_field' et 'recaptcha_response_field'. Passez les alors à la méthode
verify() :
verify(
$_POST['recaptcha_challenge_field'],
$_POST['recaptcha_response_field']
);
]]>
Une fois que vous possédez le résultat, vérifiez sa validité. Il s'agit d'un objet
Zend_Service_ReCaptcha_Response qui possède une méthode
isValid().
Vérifier les champs de formulaireisValid()) {
// Validation échouée
}
]]>
Encore plus simple : utilisez l'adaptateur ReCaptcha de
Zend_Captcha, ou utilisez cet adaptateur comme backend pour l'élément formulaire Captcha. Dans
ces 2 cas, le rendu et la validation du reCAPTCHA sont assurés pour vous.
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, and 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 windows 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 it as the fourth argument to the
constructor or by calling the 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 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);
}
]]>