This article mainly introduces the implementation of the thinkphp verification code (form and ajax verification). For more information, see the following two verification codes.
, One is directly verified by the form submission button, and the other is verified by passing parameters through ajax:
1. perform verification directly on the form submission button and write the following code in the controller VerifyController. class. php:
Namespace Home \ Controller; use Think \ Controller; class VerifyController extends Controller {public function index () {$ this-> display ();} public function checkLogin () {$ verify = new \ Think \ Verify (); $ code = I ('post. verify '); // form verification code if ($ verify-> check ($ code) {$ this-> success ('verification code right ');} else {$ this-> error ('verification code error');} public function verify () {// instantiate the Verify object $ verify = new \ Think \ Verify (); // Configure the verification code parameter $ verify-> fontSize = 14; // the verification code font size $ verify-> length = 4; // The number of digits of the verification code $ verify-> imageH = 34; // verification code height $ verify-> useImgBg = true; // enable the verification code background $ verify-> useNoise = false; // disable the verification code interference Miscellaneous $ verify-> entry ();}}
The code in Verify/index.html is as follows:
2. use ajax to pass parameters for verification. the code in controller VerifyController. class. php is as follows:
Namespace Home \ Controller; use Think \ Controller; class VerifyController extends Controller {public function index () {$ this-> display ();} public function checkLogin () {$ verify = new \ Think \ Verify (); $ code = $ _ POST ['code']; // Obtain the ajax verification code if ($ verify-> check ($ code) {$ this-> ajaxReturn (1 );} else {$ this-> ajaxReturn (0) ;}} public function verify () {// instantiate the Verify object $ verify = new \ Think \ Verify (); // Configure the verification code parameter $ verify-> fontSize = 14; // the verification code font size $ verify-> length = 4; // The number of digits of the verification code $ verify-> imageH = 34; // verification code height $ verify-> useImgBg = true; // enable the verification code background $ verify-> useNoise = false; // disable the verification code interference Miscellaneous $ verify-> entry ();}}
The code in Verify/index.html is as follows: