Freelancer Martin Zeller

php asp.net java c# .net c++ xml xhtml seo magento zend framework dotnetnuke tomcat iis sql server mysql oracle typo3 coremedia

Zend_Captcha, an example without Zend_Form

Tags: ,

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

In your action method:

public function indexAction()
{
//…
$captcha = new Zend_Captcha_Image();
$captcha->setImgDir(APPLICATION_PATH . ‘/../public/tmp/captcha/’);
$captcha->setImgUrl($this->view->baseUrl(’/tmp/captcha/’));
$captcha->setFont(APPLICATION_PATH . ‘/fonts/elephant.ttf’);
$captcha->setWidth(350);
$captcha->setHeight(150);
$captcha->setWordlen(5);
$captcha->setFontSize(70);
$captcha->setLineNoiseLevel(3);
$captcha->generate();
$this->view->captcha = $captcha;   // giving captcha object to the view
//…
}

In the corresponding view render the captcha and add a hidden field with the captcha id:

<form …
<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>

Validating the input of the user after postback:

// …
$capId = $_POST['cid'];
$capSession = new Zend_Session_Namespace(’Zend_Form_Captcha_’.$capId);
if ($_POST['captcha']==$capSession->word)
{
// input OK
}
else {
// input NOK
}
// …

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

Tags: ,

Leave a Reply

© 2009 Freelancer Martin Zeller. All Rights Reserved.

This blog is powered by Wordpress and Magatheme by Bryan Helmig.