Thinkphp verification code, thinkphp verification code is not displayed
Thinkphp built-in verification code
Front-end page:
<div style="position:absolute;z-index:3;top:160px;left:180px;"> </div>
// Public function Verify () {ob_clean (); // display the verification code $ cfg = array ('codeset' => '2016 ', // The Verification Code character set is in combination with 'imageh '=> 25, // The Verification Code Image Height Is 'imagew' => 80, // The Verification Code image width is 'length' => 4, // number of digits of the Verification Code 'fontttf' => '4. ttf', // the font of the Verification Code. Do not set the random 'fontsize' => 10, // The font size of the Verification Code (px) 'usenoise '=> false, // whether to add the noise point 'usecurve' => false, // whether to draw the confusion curve 'bg '=> array (226,229,236) // background color ); $ very = new \ Think \ Verify ($ cfg); $ very-> entry ();} // The client verifies the verification code public function checkVerify () through ajax () {$ code = I ('get. code '); $ very = new \ Think \ Verify (); $ key = $ this-> auth_my_code ($ very, $ very-> seKey ); // The Verification code cannot be blank $ secode = session ($ key); // encrypt $ code. if ($ this-> auth_my_code ($ very, strtoupper ($ code) = $ secode ['verify _ Code']) {echo json_encode (array ('flag' => 1, 'cont' => 'correct Verification Code ');} else {echo json_encode (array ('flag' => 2, 'cont' => 'verification code error');} private function auth_my_code ($ vry, $ str) {$ key = substr (md5 ($ vry-> seKey ), 5, 8); $ str = substr (md5 ($ str), 8, 10); return md5 ($ key. $ str );}
If the verification code above is incorrectly entered and cannot be automatically refreshed after submission, after the code is changed:
Location. href = "/Login"; in this way, you can only refresh the entire page. The value of the submitted form may be lost, which affects the user experience.
2. If the verification code is incorrect, the verification code is automatically refreshed after submission.
Else {$ ('# safecode'). attr ("src", "/Login/Verify? "+ Math. random (); NewAlert (2, "Incorrect verification code, please enter", null); code_ OK = false; optional ('{verifyresult'{.html(msg.cont}.css ({'color': 'red ', 'font-size': '12px '});}
3. The following is the verification code submitted by ajax to the backend for verification:
<Script type = "text/javascript"> $ ("# login_btn "). click (function () {var username = $. trim ($ ("# username "). val (); var password = $. trim ($ ("# password "). val (); var code = $ ('# veri '). val (); if (username = "") {NewAlert (2, "Enter the user name", null); shutdown (); return false ;} else if (password = "") {NewAlert (2, "Enter password", null); shutdown (); return false;} else if (code = '') {NewAlert (2, "Enter the verification code", null); return false;} // Jax goes to the server side for verification $. ajax ({url: "_ CONTROLLER _/checkVerify", data: {'code': code}, dataType: 'json', success: function (msg) {if (msg. flag = 1) {var data = {username: username, password: password}; $. ajax ({type: "POST", url: "{: U ('login/login')}", data: data, dataType: "json", success: function (msg) {if (msg. respCode = '000') {shutdown (); if (msg.org _ code = 'fcb ') {location. href = "/Invest/index? Biao_type = cwb ";} else {location. href = "{$ Think. config. VIP_URL}/Individual/index ";} else {NewAlert (2, msg. respDesc, null); return false ;}, error: function () {shutdown () ;}, beforeSend: function () {Loading ();},});} else {$ ('# safecode '). attr ("src", "/Login/Verify? "+ Math. random (); NewAlert (2, "Incorrect verification code, please enter", null); code_ OK = false; optional ('{verifyresult'{.html(msg.cont}.css ({'color': 'red ', 'font-size': '12px '}) ;}}}) ;}); </script>