我需要在ZF应用程序中的表单中插入ReCaptcha。我正在尝试跟踪官方文档,但是ReCaptcha服务总是返回错误‘不正确的-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
}
有什么帮助吗?
发布于 2009-12-12 19:38:32
为什么要从表单中提取一个单独的元素来进行检查?我就是这样做的:
表单
<?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。
当然,请确保所使用的密钥与运行此代码的域相关。
如果你还有其他问题,请告诉我。
发布于 2010-11-06 14:10:57
我跟随着zend遗址的快速起步,对我来说,下面的变化比'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'
)
));
https://stackoverflow.com/questions/1890622
复制相似问题