使用Zend_Captcha生成验证码的方法
时间:2021-07-01 10:21:17
帮助过:18人阅读
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';
- }
- }
- }
- }
- ?>
|