Zend Framework 现在已经是1.80版本了,出现了一些新组建,感觉强大了许多。今天测试了下Zend_Captcha, 手册中的示例有点问题,下面是可以工作的代码。
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';
}
}
}
}
阅读(1678) | 评论(0) | 转发(0) |