Zend - Recaptcha 奇怪的问题 - 工作但不符合预期
我有一个有点奇怪的问题。我正在测试 Zend,需要将 reCaptcha 字段添加到表单中。我按照 Zend 文档中给出的示例进行操作,但它不起作用(已经习惯了)。收到“不正确的验证码-sol”错误。
经过一段时间的阅读后,我终于成功了。然而, isValid 方法返回的结果似乎与您的预期相反。
下面是代码:
Form:
class Application_Form_Album extends Zend_Form {
public function init() {
## Set Recapture
$this->setName('album');
$this->setMethod('POST');
$id = new Zend_Form_Element_Hidden('id');
$id->addFilter('Int');
$artist = new Zend_Form_Element_Text('artist');
$artist->setLabel('Artist')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$title = new Zend_Form_Element_Text('title');
$title->setLabel('Title')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$submit = new Zend_Form_Element_Submit('submit');
$submit->setAttrib('id', 'submitbutton');
//Change theme
$recaptcha = new Zend_Service_ReCaptcha("XXXXXXX","XXXXXXX");
$recaptcha->setOption('theme', 'clean');
$captcha = new Zend_Form_Element_Captcha('challenge', array('captcha' => 'ReCaptcha','captchaOptions' => array('captcha' => 'ReCaptcha','service' => $recaptcha)));
$this->addElements(array($id, $artist, $title, $captcha, $submit));
}
}
和控制器方法:
public function addAction()
{
$auth = Zend_Auth::getInstance();
if ($auth->hasIdentity()) {
$form = new Application_Form_Album();
$form->submit->setLabel('Add');
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
$captcha = new Zend_Service_ReCaptcha("XXXXXXX","XXXXXXX");
$result = $captcha->verify($this->_getParam('recaptcha_challenge_field'),
$this->_getParam('recaptcha_response_field'));
if ($result->isValid()) {
//ReCaptcha validation error
#echo "CAPTCHA FAILED!<br>";
} else {
$artist = $form->getValue('artist');
$title = $form->getValue('title');
$albums = new Application_Model_DbTable_Albums();
$albums->addAlbum($artist, $title);
$this->_helper->redirector('index');
}
} else {
$form->populate($formData);
}
}
} else {
$this->_helper->redirector('index','auth');
}
}
我会假设 ($result->isValid()) 在输入有效验证码时返回 TRUE。经过一番拉扯后,我发现当验证码成功输入时, $result->isValid() 返回 FALSE ,如果输入错误的单词或没有输入单词,则返回 TRUE ?
我错过了什么吗?有谁知道为什么会发生这种情况?
I have a bit of a strange problem. I am testing out Zend and needed to add a reCaptcha field to a form. I followed the example given in the Zend documentation it didn't work (getting used to that). Was getting the 'incorrect-captcha-sol' error.
After reading around for a while I finally managed to get it work. However it seems that the isValid method is returning the opposite from what you would expect.
Here is the code:
Form:
class Application_Form_Album extends Zend_Form {
public function init() {
## Set Recapture
$this->setName('album');
$this->setMethod('POST');
$id = new Zend_Form_Element_Hidden('id');
$id->addFilter('Int');
$artist = new Zend_Form_Element_Text('artist');
$artist->setLabel('Artist')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$title = new Zend_Form_Element_Text('title');
$title->setLabel('Title')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$submit = new Zend_Form_Element_Submit('submit');
$submit->setAttrib('id', 'submitbutton');
//Change theme
$recaptcha = new Zend_Service_ReCaptcha("XXXXXXX","XXXXXXX");
$recaptcha->setOption('theme', 'clean');
$captcha = new Zend_Form_Element_Captcha('challenge', array('captcha' => 'ReCaptcha','captchaOptions' => array('captcha' => 'ReCaptcha','service' => $recaptcha)));
$this->addElements(array($id, $artist, $title, $captcha, $submit));
}
}
And the Controller method:
public function addAction()
{
$auth = Zend_Auth::getInstance();
if ($auth->hasIdentity()) {
$form = new Application_Form_Album();
$form->submit->setLabel('Add');
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
$captcha = new Zend_Service_ReCaptcha("XXXXXXX","XXXXXXX");
$result = $captcha->verify($this->_getParam('recaptcha_challenge_field'),
$this->_getParam('recaptcha_response_field'));
if ($result->isValid()) {
//ReCaptcha validation error
#echo "CAPTCHA FAILED!<br>";
} else {
$artist = $form->getValue('artist');
$title = $form->getValue('title');
$albums = new Application_Model_DbTable_Albums();
$albums->addAlbum($artist, $title);
$this->_helper->redirector('index');
}
} else {
$form->populate($formData);
}
}
} else {
$this->_helper->redirector('index','auth');
}
}
I would have assumed ($result->isValid()) to return TRUE on a valid captcha being entered. After some hair pulling I figured $result->isValid() is returning FALSE when captcha was successfully entered and TRUE if the wrong words or no words were entered?
Am I missing something? Anyone know why this could be happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您不需要在控制器中创建新的 Zend_Service_ReCaptcha 。表格应该照顾它。尝试从表单中获取验证码元素并查看其是否有效,而不是
if ($result->isValid()) {
。查看 http://framework.zend.com/ 中的示例Manual/en/zend.captcha.operation.html它可能看起来像:
I don't think you need to create a new Zend_Service_ReCaptcha in your controller. The form should be taking care of it. Instead of
if ($result->isValid()) {
, try getting the captcha element from the form and seeing if THAT is valid. Take a look at the example in http://framework.zend.com/manual/en/zend.captcha.operation.htmlIt might look something like: