This article mainly introduces ZendFramework's function of generating a verification code and verifying the verification code. A more detailed analysis shows the specific steps and related skills for ZendFramework to implement an operation verification code, the demo source code is provided for readers to download and reference. If you need it, you can refer to the following example to describe 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.