教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 使用Zend_Captcha生成验证码的方法

使用Zend_Captcha生成验证码的方法

发布时间:2015-01-16   编辑:jiaochengji.com
测试了下zf中的Zend_Captcha,手册中的示例有点问题,以下是测试成功的代码。

测试了下zf中的Zend_Captcha,手册中的示例有点问题,以下是测试成功的代码。
 

复制代码 代码如下:

<?php
class TestController extends Lyw0301_Controller_Action {
 public function init() {
  parent::init();
  $this->view->title = '测试';
  $this->view->baseUrl = $this->getFrontController()->getBaseUrl();
  // $this->_helper->viewRenderer->setNoRender();
  //Zend_Layout::getMvcInstance()->disableLayout();
 }
 function generateCaptcha() {
  $captcha = new Zend_Captcha_Image();
  $captcha->setTimeout('300')
  ->setWordLen('6')
  ->setHeight('80')
  ->setFont('./images/font/micross.ttf')
  ->setImgDir('./images/code');
 
  $captcha->generate();  
  return $captcha->getId();
 } 

 //validates captcha response
 function validateCaptcha($captcha) {
  $captchaId = $captcha['id'];
  $captchaInput = $captcha['input'];
  $captchaSession = new Zend_Session_Namespace('Zend_Form_Captcha_' . $captchaId);
  $captchaIterator = $captchaSession->getIterator();
  Zend_Debug::dump($captchaIterator);exit;
  $captchaWord = $captchaIterator['word'];
  if($captchaWord) {
   if( $captchaInput != $captchaWord ){
    return false;
   } else {
    return true;
   }
  } else {
   return false;
  }
 }
 public function indexAction() {
  $captchaId = $this->generateCaptcha();      
  $this->view->captchaId = $captchaId;
  if(isset($_POST['captcha'])) {     
   $captcha = $_POST['captcha'];   
   if( $this->validateCaptcha($captcha) ) {
    $this->view->message = 'yes';
   } else {
    $this->view->message = 'no';
   }
  }
 }
}
?>

您可能感兴趣的文章:
使用Zend_Captcha生成验证码的方法
php验证码大全(实例分享)
php彩色验证码的简单例子
用php生成带有雪花背景的验证码
php点击验证码实时刷新的实现代码
php5验证码类(简易实用型)
php验证码的三个实例代码分享
php生成扭曲及旋转的验证码图片的实例代码
php图片验证码的例子
php验证码简单函数代码(附效果图)

关键词: Captcha  php验证码  验证码   
[关闭]
~ ~