Zend_Captcha, an example without Zend_Form

Martin Zeller php 2 Comments

Here is your example how to implement Zend_Captcha_Image without using Zend_Form.

In your action method:
[php]public function indexAction()
$captcha = new Zend_Captcha_Image();
$captcha->setImgDir(APPLICATION_PATH . ‘/../public/tmp/captcha/’);
$captcha->setFont(APPLICATION_PATH . ‘/fonts/elephant.ttf’);
$this->view->captcha = $captcha;   // giving captcha object to the view
}[/php] In the corresponding view render the captcha and add a hidden field with the captcha id:
<input id="captcha" type="text" name="captcha" />
<?php echo $this->captcha->render($this, null) ?>
<input type="hidden" name="cid" value="<?php echo $this->captcha->getId() ?>" >
</form>[/php] Validating the input of the user after postback:
[php]// …
$capId = $_POST[‘cid’];
$capSession = new Zend_Session_Namespace(‘Zend_Form_Captcha_’.$capId);
if ($_POST[‘captcha’]==$capSession->word)
// input OK
else {
// input NOK
// …[/php] Good luck!

PS: If you want to use your Zend_Captcha WITH Zend_Form, this blog post may help you: Zend_Captcha with Zend_Form

Comments 2

  1. Pingback: Zend_Captcha ohne Zend_Form | Keleo

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.