2009-12-13 85 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 
    }