How to use Zend_Captcha to generate a verification code
Class TestController extends Lyw0301_Controller_Action {
- Public function init (){
- Parent: init ();
- $ This-> view-> title = 'test ';
- $ This-> view-> baseUrl = $ this-> getFrontController ()-> getBaseUrl ();
- // $ This-> _ helper-> viewRenderer-> setNoRender ();
- // Zend_Layout: getMvcInstance ()-> disableLayout ();
- }
- Function generateCaptcha (){
- $ Captcha = new Zend_Captcha_Image ();
- $ Captcha-> setTimeout ('20140901 ')
- -> 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 ';
- }
- }
- }
- }
- ?>
|