欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

使用Zend_Captcha生成验证码的方法

程序员文章站 2022-03-30 13:20:51
...
  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. ?>
复制代码