1. Determine the project directory "vendor" Topthink "Think-captcha directory exists
2. Add the captcha configuration in config
Verification code Configuration ' Captcha ' + [ //Verification Code character Set ' codeset ' = ' 2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY ', //Captcha font size (px) ' fontSize ' + //whether to draw confusion curve ' Usecurve ' + true, //captcha Image height ' imageh ' =// whether to add a miscellaneous dot ' usenoise ' =>true, // Captcha image width ' imagew ' + 148, //Verify code bits ' Length ' = 4, //Verify success after reset ' reset ' = True ] ,
3. Output verification code in template captcha.html
<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>Verification Code</title> <Scripttype= "Text/javascript"src= "Https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></Script></Head><Body>Enter the verification code:<Div> <imgID= "Verify_img"src= "{: Captcha_src ()}"alt= "Verification Code"onclick= "refreshverify ()"> <ahref= "javascript:refreshverify ()">Click Refresh</a></Div><formclass= "Layui-form"Action="" > <inputtype= "text"name= "Verify"> <Buttonclass= "Layui-btn"Lay-filter= "Checkcaptcha"Lay-submit=""ID= "Checkcaptcha" >Save</Button></form><Script> functionrefreshverify () {varTS=Date.parse (NewDate ())/; varimg=document.getElementById ('verify_img'); IMG.SRC= "{: Captcha_src ()}"; }</Script><Script> $(function () { $("#checkcaptcha"). On ("Click",function() {$.ajax ({type:'POST', URL:"{: url (' Test/checkcaptcha ')}", Data: $ (". Layui-form"). Serialize (), DataType:"JSON", Async:false, Error:function(Request) {alert ("Send request failed! "); }, Success:function(data) {Console.log (data); if(Data.status== 1) {alert (data.message); } Else{alert (data.message); } } }); }) })</Script></Body></HTML>
4. Write the Verification Code test logic in the controller test.php
Verification code Public function Checkcaptcha () { $status =1; $captcha = input (' verify '); if (!captcha_check ($captcha)) { //captcha error $message = ' authenticode error '; } else{ //Verification code is correct $message = ' Verification code is correct '; } return [' status ' = $status, ' message ' = ' $message]; }
THINKPHP5 Verification Code Processing