Home >Backend Development >PHP Tutorial >How to generate verification code using Zend_Captcha

How to generate verification code using Zend_Captcha

WBOY
WBOYOriginal
2016-07-25 09:11:071089browse
  1. class TestController extends Lyw0301_Controller_Action {

  2. public function init() {
  3. parent::init();
  4. $this->view->title = '测试';
  5. $this->view->baseUrl = $this->getFrontController()->getBaseUrl();
  6. // $this->_helper->viewRenderer->setNoRender();
  7. //Zend_Layout::getMvcInstance()->disableLayout();
  8. }
  9. function generateCaptcha() {
  10. $captcha = new Zend_Captcha_Image();
  11. $captcha->setTimeout('300')
  12. ->setWordLen('6')
  13. ->setHeight('80')
  14. ->setFont('./images/font/micross.ttf')
  15. ->setImgDir('./images/code');
  16. $captcha->generate();
  17. return $captcha->getId();
  18. }

  19. //validates captcha response

  20. function validateCaptcha($captcha) {
  21. $captchaId = $captcha['id'];
  22. $captchaInput = $captcha['input'];
  23. $captchaSession = new Zend_Session_Namespace('Zend_Form_Captcha_' . $captchaId);
  24. $captchaIterator = $captchaSession->getIterator();
  25. Zend_Debug::dump($captchaIterator);exit;
  26. $captchaWord = $captchaIterator['word'];
  27. if($captchaWord) {
  28. if( $captchaInput != $captchaWord ){
  29. return false;
  30. } else {
  31. return true;
  32. }
  33. } else {
  34. return false;
  35. }
  36. }
  37. public function indexAction() {
  38. $captchaId = $this->generateCaptcha();
  39. $this->view->captchaId = $captchaId;
  40. if(isset($_POST['captcha'])) {
  41. $captcha = $_POST['captcha'];
  42. if( $this->validateCaptcha($captcha) ) {
  43. $this->view->message = 'yes';
  44. } else {
  45. $this->view->message = 'no';
  46. }
  47. }
  48. }
  49. }
  50. ?>

复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn