views:

297

answers:

2

Hi,

I've got an issue with Zend_Captcha always returning false when the page is submitted and the captcha's isValid() method is being called. It's driving my nuts because this as far as I am concerned should work.

I start by declaring this at the top of the action function of the controller

$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);

I do usual form validation and that all works, however it is when I come to validate that the value entered into form for the captcha it always returns 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;
}

I've check to ensure that the id that I am getting and storing as a hidden element in my form match the image that is being generated but no matter what I do this always fails.

Am I missing something simple here?? Or is there a better way of dealing with this??

Thanks,

+1  A: 

It might be something to do with sessions - maybe session values are not stored properly. Check what you have in your $_SESSION - should be something like:

  ["__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"
    }

Note that in your code you probably take $captchaId from new captcha, but it may be that in the session you still have the ID for old one. Verify that IDs really match.

If you don't, check your sessions work OK, if they do it's probably some bug - submit a bug to ZF Issue tracker.

StasM
thanks, it was a problem with $captchaId using the above helped me find the problem. Thanks
Grant Collins
A: 

it works for me

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
     }
cybernetica