Background: Said before the project, lazy login module did not add verification code, think of or add, find the next TP document, found that there is integration, special record.
First, instantiate the class that generated the verification code (this method is easy to access in Indexcontroller)
[PHP] View plaincopy
- /**
- *
- * Verification Code generation
- */
- Public function verify_c () {
- $Verify = New \think\verify ();
- $Verify ->fontsize = 18;
- $Verify ->length = 4;
- $Verify ->usenoise = false;
- $Verify ->codeset = ' 0123456789 ' ;
- $Verify ->imagew = 130;
- $Verify ->imageh = 50;
- //$Verify->expire = +;
- $Verify ->entry ();
- }
second, the foreground needs to generate a verification code image SRC attribute point to
[HTML] View plaincopy
- < P class = id = " Captcha-container " >
- < input name = "verify" width = "50%" height = "" class = "Captcha-text" placeholder = "verification Code" type = "text" >
- < img width = "30%" class = "left15" height = "" alt = "verification Code" src = title = "click to refresh" >
- </ P >
three, after the completion of the above, the page initialization verification code can appear, the following to write is to click on the verification code after the image, refresh the new verification code image (through jquery modified the SRC attribute of the image to complete, the request processing function, just add a random number after the request, A request to distinguish the previous picture)
[JavaScript] View plaincopy
- //Verification code generation
- var captcha_img = $ (' #captcha-container '). Find (' img ')
- var verifyimg = captcha_img.attr ("src");
- Captcha_img.attr (' title', ' Click to Refresh ');
- Captcha_img.click (function() {
- if (Verifyimg.indexof ('? ' ) ) {>0) {
- $ (this). attr ("src", verifyimg+' &random= '+math.random ());
- }Else{
- $ (this). attr ("src", Verifyimg.replace (/\). *$/,') +'? ' +math.random ());
- }
- });
Verification code input is correct
A. Add a global function to the function.php in the common directory
[PHP] View plaincopy
- /**
- * Verification Code Check
- */
- function check_verify ($code, $id = "") {
- $verify = New \think\verify ();
- return $verify->check ($code, $id);
- }
B. Add a check code to the handler corresponding to the form submitted by the Controller
[PHP] View plaincopy
- //Check the verification code
- $verify = I (' param.verify ',');
- if (!check_verify ($verify)) {
- $this ->error ("Pro, the verification code is wrong!") ",$this->site_url,9);
- }
The
use of this tp3.2 verification code is possible.
Add: When I write, I put four of the B-Step into an AJAX validation, returning a test result. Then based on the return of the results to determine whether to submit the form, but after the verification code through the first check, the second will not be able to, at present, do not want to understand why.
thinkphp3.2 Verification code generation and click Refresh Verification Code