当前位置:Gxlcms > PHP教程 > 使用Zend_Captcha生成验证码的方法

使用Zend_Captcha生成验证码的方法

时间:2021-07-01 10:21:17 帮助过:18人阅读

  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. ?>

人气教程排行