Zend 框架和 ReCaptcha [英] Zend framework and ReCaptcha

查看:42
本文介绍了Zend 框架和 ReCaptcha的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

(在表格中)

// 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);

(在控制器中)

$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
}

有什么帮助吗?

推荐答案

为什么要从表单中拉出一个单独的元素来进行检查?我就是这样做的:

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

表格

<?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'));
   }
}

控制器

$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

查看

echo $this->form;

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

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.

这篇关于Zend 框架和 ReCaptcha的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆