Zend框架和ReCaptcha

Ste*_*ano 7 zend-framework recaptcha

我需要在我的ZF应用程序中的表单中插入ReCaptcha.我正在尝试遵循官方文档,但ReCaptcha服务总是返回错误'wrong-captcha-sol'.我正在使用的代码:

(在表格中)

// 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);
Run Code Online (Sandbox Code Playgroud)

(在控制器中)

$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
}
Run Code Online (Sandbox Code Playgroud)

有什么帮助吗?

Pav*_*nin 20

为什么要从表单中提取单独的元素进行检查?这就是我这样做的方式:

形成

<?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'));
   }
}
Run Code Online (Sandbox Code Playgroud)

调节器

$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
Run Code Online (Sandbox Code Playgroud)

视图

echo $this->form;
Run Code Online (Sandbox Code Playgroud)

这是非常透明的代码.当执行form的isValid()时,它会验证其所有元素,并且只有在每个元素都有效时才返回true.

当然,确保您使用的密钥与运行此代码的域相关.

如果您有更多问题,请与我们联系.


Kin*_*xit 16

我跟随zend网站的快速启动,对我来说,以下是'Figlet'验证码的更快更改.

   $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'
        )
    ));
Run Code Online (Sandbox Code Playgroud)

  • +1 - 比接受的答案简单得多 (4认同)