我需要在我的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);
(在控制器中)
$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
}
请帮忙吗?
答案 0 :(得分: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'));
}
}
<强>控制器强>
$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;
这里的代码非常透明。当执行form的isValid()时,它会验证其所有元素,并且只有在每个元素都有效时才返回true。
当然,确保您使用的密钥与运行此代码的域相关。
如果您有更多问题,请与我们联系。
答案 1 :(得分:16)
我跟随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'
)
));