English 中文(简体)
zend_captcha always fails isValid()
原标题:

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,

最佳回答

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.

问题回答

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
     }




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签