2009-12-13 118 views
2

我有一个Zend_Captcha问题,当页面被提交并且验证码的isValid()方法被调用时,它总是返回false。这是在推动我的坚果,因为就我而言,这应该起作用。zend_captcha总是失败isValid()

我通过在控制器

$captcha = new Zend_Captcha_Image('captcha', 
    array(
     'captcha' => array(
      'name' => 'graduatesignupcaptcha', 
      'wordlen' => 6,  
      'font' => $this->config->captcha->font, 
      'imgDir' => $baseUrl.'/images/captcha/', 
      'imgUrl' => $this->config->webserver->name.'/images/captcha/', 
     ) 
    ) 
); 
$captcha->setHeight(80) 
     ->setTimeout(300); 

我做平常表单验证的作用函数的顶部声明这一点,所有的作品开始,但它是我来的时候,以验证值输入到表格对于验证码,它总是返回false。

//next we check the captcha text to ensure that the form is a person not a script  
$captchaText = $form->getElement('captchainput')->getValue(); 
$captchaId = $form->getElement('captchaid')->getValue(); 
//$captchaSession = new Zend_Session_Namespace('Zend_Form_Captcha_'.$captchaId); 


$captchaArray = array(
'id' => $captchaId, 
'input' => $captchaText  
); 



if(!$captcha->isValid($captchaArray)){ 

$log->log(implode(",",$captcha->getErrors()), Zend_Log::DEBUG); 

$form->getElement('captchainput')->setErrors(array('messages' => 'Bad security code'));  
$formFailed = true; 
} 

我检查,以确保该我得到和储存在我的形式正在生成图像匹配一个隐藏的元素是我做这个ID,但无论总是失败。

我是否缺少一些简单的东西?还是有更好的方式来处理这个?

谢谢,

回答

2

这可能与会话有关 - 也许会话值没有正确存储。请你在你的$_SESSION - 应该是这样的:

 
    ["__ZF"]=> 
    array(1) { 
    ["Zend_Form_Captcha_ef828f68e467db99e8f358244ad6c667"]=> 
    array(2) { 
     ["ENNH"]=> 
     int(1) 
     ["ENT"]=> 
     int(1260764250) 
     } 
    } 
    ["Zend_Form_Captcha_ef828f68e467db99e8f358244ad6c667"]=> 
    array(1) { 
     ["word"]=> 
     string(6) "fubara" 
    } 

注意,在你的代码可能是由新的captcha采取$ captchaId,但它可能是在会话中,你仍然有旧的ID 。确认ID确实匹配。

如果你不这样做,请检查你的会话是否正常工作,如果他们这样做可能是一些错误 - 向ZF问题跟踪器提交一个错误。

+0

感谢,它使用上述帮我找到问题是与$ captchaId问题。谢谢 – 2009-12-14 21:20:30

0

它为我工作

Zend_Loader::loadClass('Zend_Session_Namespace'); 
$sessionNamespace = new Zend_Session_Namespace('cptc'); 



Zend_Loader::loadClass('Zend_Captcha_Image'); 
$captcha = new Zend_Captcha_Image(); 
$captchaArray = array(
     'id' => $sessionNamespace->code, 
     'input' => $filter->filter($this->_request->getParam('captcha'))  
      ); 
    if ($captcha->isValid($captchaArray)) { 
    //your action here 
    }