首页 > 后端开发 > php教程 > 使用Zend_Captcha生成验证码的方法

使用Zend_Captcha生成验证码的方法

WBOY
发布: 2016-07-25 09:11:07
原创
1080 人浏览过
  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. ?>
复制代码


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板