【问题标题】:Zend framework and ReCaptchaZend 框架和 ReCaptcha
【发布时间】:2009-12-11 20:21:01
【问题描述】:

我需要在我的 ZF 应用程序的表单中插入 ReCaptcha。我正在尝试遵循官方文档,但 ReCaptcha 服务总是返回错误“不正确的验证码溶胶”。 我正在使用的代码:

(在表格中)

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

有什么帮助吗?

【问题讨论】:

    标签: zend-framework recaptcha


    【解决方案1】:

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

    表格

    <?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。

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

    如果您还有其他问题,请告诉我。

    【讨论】:

      【解决方案2】:

      我正在关注the zend site 的快速入门,对我来说,以下是与“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'
              )
          ));
      

      【讨论】:

      • 这应该是公认的答案。我用过,效果很好。 reCaptcha 的最简单方法。谢谢哥们;)
      猜你喜欢
      • 1970-01-01
      • 2012-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-26
      相关资源
      最近更新 更多