English 中文(简体)
Zend framework and ReCaptcha
原标题:

I need to insert ReCaptcha in a form in my ZF application. I m trying to follow the official documentation, but the ReCaptcha service return me always the error incorrect-captcha-sol . The code I m using:

(In the form)

// configure the captcha service
$privateKey =  XXXXXXXXXXXXXXXXXXX ;
$publicKey =  YYYYYYYYYYYYYYYYYYYY ;
$recaptcha = new Zend_Service_ReCaptcha($publicKey, $privateKey);

// create the captcha control
$captcha = new Zend_Form_Element_Captcha( captcha ,
                                array( captcha  =>  ReCaptcha ,
                                       captchaOptions  => array(
                                           captcha  =>  ReCaptcha ,
                                           service  => $recaptcha)));

$this->addElement($captcha);

(In the controller)

$recaptcha = new Zend_Service_ReCaptcha( YYYYYYYYYYYYY ,  XXXXXXXXXXXXXXX );

$result = $recaptcha->verify($this->_getParam( recaptcha_challenge_field ),
                             $this->_getParam( recaptcha_response_field ));

if (!$result->isValid()) {
    //ReCaptcha validation error
}

Any help please?

最佳回答

Why do you pull a separate element from the form to make a check? This is how I do this:

Form

<?php
class Default_Form_ReCaptcha extends Zend_Form
{
    public function init()
    {
        $publickey =  YOUR KEY HERE ;
        $privatekey =  YOUR KEY HERE ;
        $recaptcha = new Zend_Service_ReCaptcha($publickey, $privatekey);

        $captcha = new Zend_Form_Element_Captcha( captcha ,
            array(
                 captcha        =>  ReCaptcha ,
                 captchaOptions  => array( captcha  =>  ReCaptcha ,  service  => $recaptcha),
                 ignore  => true
                )
        );

        $this->addElement($captcha);

        $this->addElement( text ,  data , array( label  =>  Some data ));
        $this->addElement( submit ,  submit , array( label  =>  Submit ));
   }
}

Controller

$form = new Default_Form_ReCaptcha();

if ($this->getRequest()->isPost()===true) {
    if($form->isValid($_POST)===true) {
        $values = $form->getValues();
        var_dump($values);
        die();
    }
}

$this->view->form = $form

View

echo $this->form;

This is quite transparent code here. When form s isValid() is executed, it validates all its elements and returns true only if each of those is valid.

An of course ensure that the keys you re using are relevant to the domain where you run this code.

Let me know if you have more questions.

问题回答

I was following the quick start at the zend site, and for me the following was much quicker change from the Figlet captcha.

   $this->addElement( captcha ,  captcha , array(
         label  =>  Please enter two words displayed below: ,
         required  => true,
         captcha  => array(
             pubkey  =>  ---your public key here--- ,
             privkey  =>  ---your private key here--- ,
             captcha  =>  reCaptcha 
        )
    ));




相关问题
Zend 邮件问题,涉及外国char子+ com子

泽斯德邮局在名称被定为具有外国性质(如“保”)和 com(”)的物品时,就放弃了一种例外(因为邮局(邮局)退回假)。 重新提出以下守则。

PHP Framework: Ebay Like Site

I am going to be builiding a site like ebay - with all the features of ebay. Please note my payment method is limited to paypal. What would be the best PHP framework to use to build this quickly, ...

Link to a specific step in onepage checkout

Is it possible to redirect the browser to nth step in the onepage checkout? If so, how would one go about doing it? I m working on a payment module and have a sort of "cancel" action that i would ...

Tagging with Zend Framework

I m trying to create the same solutions as below, but using a simple MySQL query (instead of the static version used below where the words/tags are implemented in the code). The name of the MySQL ...

dynamicaly adding textboxes to zend_form

I hope that this is a quick question to answer. I am developing a form using Zend_Form, I have a number of Zend_Dojo_Form_Element_Textboxs to add to this form dynamically. These are added from rows ...

热门标签