26-12-2011

PHP Zend Captcha image example

This is just a quick example of how you can deploy Zend Framework Captcha image component in a form.

This is the action inside the controller:

function turingtestAction(){
		if (isset($_POST['cid'])){	
			$capId = trim($_POST['cid']);
			$capSession = new Zend_Session_Namespace('Zend_Form_Captcha_'.$capId);
			if ($_POST['captcha'] == $capSession->word)
			{
				//succesful  test - probably a human
				$this->view->human = 1;
 
				return;  // end action execution here
			}
		} 
 
		$captcha = new Zend_Captcha_Image();
		$captcha->setImgDir(APPLICATION_PATH . '/../public/img/captcha/');
		$captcha->setImgUrl($this->view->baseUrl('/img/captcha/'));
		$captcha->setFont(APPLICATION_PATH . '/../public/css/LeagueGothic/League_Gothic-webfont.ttf');
		$captcha->setWordlen(5);
		$captcha->setFontSize(28);
		$captcha->setLineNoiseLevel(3);
		$captcha->setWidth(90);
		$captcha->setHeight(64);
		$captcha->generate();
		$this->view->captcha = $captcha;
}
 

This is the view code (turingtest.phtml):

<?php
if (isset($this->captcha)){ 
?><form method = "post" action = "<?= $this->baseUrl() ?>/auth/turingtest/">
	<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 
}
if (isset($this->human)){ 
	echo  'human';
}?>
 
Keywords: captcha

Comments:

Your comment:

»
chirag 03/02/2012, 11:47 am
fine

 

[x]