Wednesday, January 4, 2012

Zend image captcha

This is just a quick example of Zend Framework Captcha image component in a form.

This is the action inside the controller :


function captchatestAction(){
  if (isset($_POST['cid'])){ 
   $capId = trim($_POST['cid']);
   $capSession = new Zend_Session_Namespace('Zend_Form_Captcha_'.$capId);
   if ($_POST['captcha'] == $capSession->word)
   {
    //success 
    $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/Arial.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 (captchatest.phtml) :

<?php
if (isset($this->captcha)){ 
?><form method = "post" action = "<?= $this->baseUrl() ?>/index/captchatest/">
 <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';
}?>

 

No comments:

Post a Comment