Zend Framework generates a verification code and implements the verification code verification function (with the demo source code download), zenddemo

Source: Internet
Author: User
Tags zend framework

Zend Framework generates a verification code and implements the verification code verification function (with the demo source code download), zenddemo

This example describes how Zend Framework generates a verification code and implements the verification code function. We will share this with you for your reference. The details are as follows:

Today we will show you how to implement the verification code function in the message book .. this will help our website reduce some spam or ad messages .. in this case, I have not used the Zend_Captcha component that comes with Zend Framework .. I just found the verification code class in the ThinkPHP framework .. I used it .. the implementation of the verification code in our message book... here I am .. is to write it into a plug-in form... of course, Zend Framework has many ways to use other code .. I will only use one of them now... so friends with good methods .. can be discussed together .. let's make progress together .. haha ....

Step 1: Find the library folder in our original tutorial .. this is where the Zend class library folder is stored .. now we add several folders in the project in the library Folder: library/Custom/Controller/Plugin/

Folder is added. at this time, we will find our verification code file .. here I name it ImgCode. php file name .. this file is the class file we will use to generate the verification code. of course, this file is changed to the plug-in form in Zend Framework... imgCode. some php files are described as follows:

<? Php // ImgCode. php program description // The verification code data generated by the program $ randval = $ this-> rand_string ($ length, $ mode); // store the data in Zend_Session... // here is a question about the Session namespace. You can check out the manual $ authCode = new Zend_Session_Namespace ('auth _ Code'); $ authCode-> imagecode = $ randval;?>

Step 2: Add the following Action to the Controller of our message book (IndexController. php) to generate the verification application.

The program is as follows:

<? Phppublic function imgcodeAction () {$ this-> getFrontController ()-> setParam ('noviewrenderer', true); // call our verification code class Zend_Loader :: loadClass ('m M _ Controller_Plugin_ImgCode '); $ imagecode = new Custom_Controller_Plugin_ImgCode (); // return the verification code image $ imagecode-> image2 ();}?>

Step 3: add the verification code function to our message template: Open the template on the message book display page, message/index. in phtml, add the following html code in the message area .. please refer to the JS Code in the source file at this time .. functions like replacing Verification Code pictures .. I won't say much about this little thing .. I just added some JS functions more than in the previous tutorial ..

Last .. in our controller (IndexController. in php), find the messageAction function .. we need to use the Verification Code function .. that is to say, to check whether the verification code entered by the user is the same as the verification code in our Session .. if the same .. let him leave a message .. otherwise, he will not be asked to leave a message .. I think everyone has this basic idea.

Click here to download the complete instance code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.