RSSAmplifier

Luca Corbo – · Apr 23, 2009

Recaptcha With Zend Form

0
Sign in to vote or save

This page cannot be shown here. You can still read it on the original site — the toolbar below keeps your place in the directory.

Requirements Zend Framework (1.7.8 at writing time) Valid ReCaptcha keys (public & private) Simple ReCaptcha Zend Form Class <? php class Form_ReCaptcha extends Zend_Form { public function init () { $this -> setMethod ( 'post' ); //Add your elements here... $recaptcha = new Zend_Service_ReCaptcha ( $publickey , $privatekey ); $captcha = new Zend_Form_Element_Captcha ( 'challenge' , array (…

Requirements

Simple ReCaptcha

Zend Form Class

<?php
class Form_ReCaptcha extends Zend_Form
{
 public function init()
 {
 $this->setMethod('post');
 //Add your elements here...

 $recaptcha = new Zend_Service_ReCaptcha($publickey, $privatekey);

 $captcha = new Zend_Form_Element_Captcha('challenge',
 array('captcha' => 'ReCaptcha',
 'captchaOptions' => array('captcha' => 'ReCaptcha', 'service' => $recaptcha)));

 $this->addElement($captcha);

 // Add the submit button
 $this->addElement('submit', 'submit', array('label' => 'Submit'));
 }
}

Zend Controller Class

<?php
class ReCaptchaController extends Zend_Controller_Action
{
 public function indexAction()
 {
 require_once APPLICATION_PATH . '/forms/Contact.php';

 $form = new Form_ReCaptcha();

 if ($this->_request->isPost()) {
 $formData = $this->_request->getPost();
 if ($form->isValid($formData)) {
 $recaptcha = new Zend_Service_ReCaptcha($publickey, $privatekey);

 $result = $recaptcha->verify($this->_getParam('recaptcha_challenge_field'),
 $this->_getParam('recaptcha_response_field'));
 if (!$result->isValid()) {
 //ReCaptcha validation error
 //Your action here...
 }
 }
 }
 $this->view->form = $form;
 }
}

Customized ReCaptcha

You may also want to internationalizing or change colors to ReCaptcha, to do it you need to specify some options for the Zend_Service_ReCaptcha object. See the ReCaptcha wiki for a complete list of available options.

Zend Form Class

<?php
class Form_ReCaptcha extends Zend_Form
{
 public function init()
 {
 $this->setMethod('post');
 //Add your elements here...
 $recaptcha = new Zend_Service_ReCaptcha($publickey, $privatekey);

 //Translate in your language
 $recaptcha_it_translation =
 array('visual_challenge' => "Verifica video",
 'audio_challenge' => "Verifica audio",
 'refresh_btn' => "Effettua una nuova verifica",
 'instructions_visual' => "Scrivi le due parole",
 'instructions_audio' => "Scrivi quello che ascolti",
 'help_btn' => "Aiuto",
 'play_again' => "Riascolto di nuovo l'audio",
 'cant_hear_this' => "Scarica l'audio come MP3",
 'incorrect_try_again' => "Incorretto. Prova ancora.");

 $recaptcha->setOption('custom_translations', $recaptcha_it_translation);
 //Change theme
 $recaptcha->setOption('theme', 'clean');

 $captcha = new Zend_Form_Element_Captcha('challenge',
 array('captcha' => 'ReCaptcha',
 'captchaOptions' => array('captcha' => 'ReCaptcha',
 'service' => $recaptcha)));

 $this->addElement($captcha);

 // Add the submit button
 $this->addElement('submit', 'submit', array('label' => 'Submit'));
 }
}

Read on /blog/recaptcha-with-zend-form/

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.