测试了下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';
}
}
}
}
?>