As a programmer of my big celestial, if do not point thinkphp frame does a little justify. But it doesn't matter, it's a simple PHP framework. Today, we introduce the function of thinkphp how to implement verification code.
Thinkphp comes with verification code function, we call directly on the line. There have been several official versions of thinkphp, and the latest version is thinkphp5.0. Today I will introduce you to the 5 version and the 3 version.
Version of THINKPHP3:
The first step: Create an output Captcha method in our corresponding controller
Public Function Verify () { $Verify = new \think\controller\verify (); $Verify->fontsize = 20; Font size $Verify->length = 6;//How many characters $Verify->usenoise = false;//whether to add a miscellaneous point $Verify->imagew = 233; Verification code Width $Verify->imageh = 50;//Verification Code height $Verify->entry ();}
Step two: Output our verification code on the page
class= "Verifycode" name= "admin_verify" title= "click Refresh Captcha" >
Complete the above operation. Next, we begin to implement additional functionality.
The first click of the verification code to implement the refresh (in fact, using an Ajax method to reload the image output): In our JS write the following method.
var verifyimg = $ (". Verifycode "); var verifypath = verifyimg. attr ("src"); verifyimg. Click (function () {if (Verifypath.indexof ("?") >0) {$ (this). attr ("src", verifypath+ ' &random= ' +math. Random ());} Else {$ (this). attr ("src", Verifypath.replace (/\?). *$/, ') + '? ' +math. random ());})}
The second is the background verification of our verification code correctness: first create a public validation function (the public configuration function file is under common, can be defined in the project's public directory can also be in the module's functional directory), and we accept the user information submitted to the place to do the validation and error feedback to the user.
Public validation functions (recommended in the project's Common directory for multiple module calls)
Public functions can be used directly in the system
function check_verify ($code$id = "") { $verifynew \think\ Verify (); return $verify->check ($code$id);}
// receive the input verification code value of the page $data [' admin_verify '] = I (' Post. Admin_verify '); if (Check_verify ($data[' admin_verify '])) { $this->error ("Incorrect Captcha", U ("Login"), 3 );}
Ah, manual valve manual valve manual valve
thinkphp Verification Code implementation.