Zend Framework Image Captcha
The Zend Framework documentation lacks when it comes to showing the ins of out of some of its components, the Zend Framework captcha seems to fall under that category. You would think there would be a ton of useful blog posts and if there are useful posts, I don’t think google is catching them. I did find some useful code on this blog, sankhomalik’s tutorial on using the zend image captcha. It does make some decent notes, so please go read this post first (and thank him, cause so far he has the best notes on the ZF captcha thus far), but the code did not seem to work with the current version of Zend that I have which is currently version 1.7.8. The other post is using some kind of Iterator and expecting the zend namespace to have a getIterator method, for some reason this was not working for me.
Here is the modified/refactored version that creates a static wrapper Captcha class using the image captcha. To reiterate what the other blog says, Make sure you have GD enabled, make sure you have a font file, make sure you have the file paths pointing to the right place, and make sure you have the right permissions for the folders that contain the images and font file.
PHP Captcha Class
class Capatcha
{
/**
* validates the last image captcha that was created
* and put into the current session.
*
* @param string $postPrefix the prefix of the post value i.e. 'captcha[id]'
* @return boolean
*/
public static function validate($postPrefix = "captcha")
{
$captcha = $_POST[$postPrefix];
$captchaId = $captcha['id'];
$captchaInput = $captcha['input'];
$captchaSession = new Zend_Session_Namespace('Zend_Form_Captcha_' . $captchaId);
$captchaWord = $captchaSession->word;
if( $captchaWord )
return $captchaInput == $captchaWord;
return false;
}
/**
* generates the captcha image and returns the image id.
*
* @param string $postPrefix the prefix of the post value i.e. 'captcha[id]'
* @return string
*/
public static function generate($postPrefix = "captcha")
{
// replace the path/to with the correct path...
// same with /images/captcha
$captcha = new Zend_Captcha_Image(array(
'name' => $postPrefix,
'wordLen' => 6,
'timeout' => 600,
'font' => "path/to/IMPACT.TTF",
'imgdir' => "path/to/images/captcha",
'imgurl' => "/images/captcha"
));
return $captcha->generate();
}
}
PHP ZF Controller
// in your action in the controller,
// it would look like some variation of this
if($this->getRequest()->isPost())
{
if(!Capatcha::validate())
{
$id = Capatcha::generate();
$this->view->captcha = $id;
$this->view->message = "captcha was invalid";
return;
} else {
$this->view->message = "saved!";
}
}
else
{
$this->view->message ="";
$id = Capatcha::generate();
$this->view->captcha = $id;
}
XHTML View
<!-- above your form using with short tags on --> <div class="error"><?= $this->message ?></div> <!-- somewhere in your form --> <img src="/images/captcha/<?= $this->captcha ?>.png" alt="captcha" /> <input type="text" name="captcha[input]" value="" /> <input type="hidden" name="captcha[id]" value="<?= $this->captcha ?>" />