The Think\verify class can support the generation and validation of verification codes.
In order to display this verification code function, the first to have a controller, then there are methods, and then the display of the page.
One, the simplest way to generate a verification code
(1) We will continue to write the method in that controller
This method shows the page of this verification code
Public Function Xianshi () { $this->show (); }
Public Function Shengcheng () { //The object that made the captcha $v = new \think\verify (); Generate Verification Code $v->entry ();}
(2) Display the Verification Code of the page, or put in, named
Run a look at the results ~ ~
(3) We can also set the size for him, every time the page is refreshed, the verification code will change
Just give him a property, for example.
"
Width and height are bigger
Second, the text box input verification code (let the text box enter the verification code and display the verification code matches)
(1) First to display the page to make a text box to show what the user sees
<div> Please enter the verification code: <input type= "text" Name= " Yzm "/></div>
We also want to do a hint of the line: used to display the message
<span id= "Tishi" ></span>
(2) Using AJAX to do this event
Trigger event when losing focus
<script type= "Text/javascript" > $ ("#yzm"). blur (function () { var Yzm = $ (this). Val ();//Find the value in this text box $.ajax ({ URL: "__action__", //Send to himself Data:{yzm:yzm},//Pass the value in the text box to the type: "POST", //Transfer Mode DataType: "TEXT", success:function (data) {<br>//Return to successful Operation }) }) </script>
(3) in the controller display Xianshi method to determine whether the verification code is correct
Public Function Xianshi () { if (empty ($_post)) { $this->show (); Show Page } else { //Verify that the verification code is correct, you can use the check method of the Think\verify class to detect the correct input of the verification code $YZM = $_post["Yzm"]; Receives the value of the passed text box $v = new \think\verify (); if ($v->check ($YZM)) //Verify that there is a return value, so we can use if to determine { $this->ajaxreturn ("OK", "eval"); Enter the correct return OK } else { $this->ajaxreturn ("No", "eval"); Input error returned no }}}
(4) Writing in Ajax to return to a successful place
Success:function (data) { if (data.trim () = = "OK") { //returns the output prompt for $ ("#tishi") if it succeeds. html ("Verify pass!" "); $ ("#tishi"). CSS ("Color", "green"); } else { //Otherwise returns the error message $ ("#tishi"). HTML ("CAPTCHA input Error! "); $ ("#tishi"). CSS ("Color", "red");} }
Here is the experiment that runs:
The first is to enter the correct verification code, to prompt the correct information
An error message will be prompted when an error is entered or no input is entered.
Third, if a page has two verification code
(1) This shows the page to have a verification code to display the place, inside the method we are shengcheng1
"
(2) The method of the controller should also write a shengcheng1
Public Function shengcheng1 () {//The object that made the captcha $v = new \think\verify (); Generate a verification code that identifies 2 $v->entry (2); }
Note: If you have two verification codes, identify them and let them know whose verification code they are.
So the first method is also labeled
Public Function Shengcheng () {//The object that made the captcha $v = new \think\verify (); Generate a verification code that identifies 1 $v->entry (1); }
Then write an identity in the check method, which is the identity to verify, and here we're going to verify the second one.
if ($v->check ($yzm, 2)) //check parameter, one is the value in the text box, the second parameter is the identity { $this->ajaxreturn ("OK", "eval"); Enter the correct return ok}else{ $this->ajaxreturn ("No", "eval"); Input error returned no }
Look at the results and enter the second verification code
Iv. Conversion of verification codes
Want to change the verification code of the picture, in fact, is to re-request the method of generating verification code to
(1) First to replace the verification code of the image to a name, as follows
(2) Then just add a click event to this image, modify the properties to
$ ("#img2"). Click (function () {///modify some properties to modify the SRC attribute $ (this). attr ("src", "__controller__/shengcheng1");})
Run to see the results, click once will change the picture
Note: Sometimes the browser will have a bug, so we have to add a thing, let it adapt to any browser, can write
$ ("#img2"). Click (function () { var sj = math.random (); Generate random number var sz = sj.tofixed (2); Take the decimal point two bits //modify some properties on it, modify the SRC attribute $ (this). attr ("src", "__controller__/shengcheng1/a" +sz); The number of random numbers that are truncated after stitching})
So IE browser can also change the diagram of the
V. Other parameters about the verification code
All of these parameters can be converted to the verification code.
Note: There are two ways to set the parameters
One is to instantiate the incoming parameters:
$config = Array ( ' fontSize ' = + , //captcha font size ' length ' = 3, //Verify code BITS ' usenoise ' = false,//Turn off verification code clutter); $Verify = new \think\verify ($config); $Verify->entry ();
The second is the dynamic setting:
$Verify = new \think\verify (), $Verify->fontsize = $Verify $Verify->length = 3;->usenoise = false;$ Verify->entry ();
Six, the verification code is Chinese
Note: In Chinese, to add a font to a folder (this folder is empty by default)
The font that comes with your computer is in a folder
Note the path where the Chinese fonts are placed: under the thinkphp/library/think/verify/zhttfs/directory
After adding the font, you can set the verification code that appears in Chinese:
Public Function shengcheng1 () { //The object that made the captcha $v = new \think\verify (); $v->fontsize =; $v->length = 2; $v->fontttf = "6.ttf"; Use a well-defined font $v->usezh = true; Use the Chinese font //To generate the verification code, the identification is 2 $v->entry (2); }
To this end, the function of verification code ~ ~ ~
All the characters used in the verification code are in the parent class, and you can see it in a short time.
Thinkphp Framework: Verification code function