thinkphp Framework: Verification Code function

Source: Internet
Author: User

I. Single Verification Code

1. The new controller TestController.class.php can still be used in the upload Function.

2. Create an action method

Namespace Home\controller;use think\controller;class TestController extends Controller {public    function Yanzheng ( {$this->show ();//display Template page}}

3. Create a new validated template page under Home/view/test yanzheng.html use Ajax to verify

<! DOCTYPE HTML Public "-//w3c//dtd XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD ">Event $ ("#btn") is triggered when the focus is lost. the click (function () {var yzm = $ ("#txt"). val ();//take The value entered by the user $.ajax ({url: "__controller__/yz", data:{ Yzm:yzm},type: "POST", dataType: "TEXT", success:function (data) {if (data.trim () = = "ok") {alert ("success");} Else{alert ("failed");}}) $ ("#img"). Click (function () {var SJS = Math.floor (math.random () *100); attr ("src", "__controller__/yzm/code/" + Sjs);}) </script>

4. Build an action method Yzm is used to generate a verification code and to have the validation button point to an action method, create a new YZ action method

<?phpnamespace home\controller;use think\controller;class TestController extends Controller {public      function Yanzheng () {$this->show ();//display template page}public function Yzm () {$v = new \think\verify ();//build OBJECT//$V->USEIMGBG = true;// Whether to use the background picture defaults to false//$v->fontsize= "30";//captcha font size (pixels) default to 25//$v->usecurve=false;//whether to use obfuscation curve default to True//$v Usenoise=false;//whether to add a miscellaneous point by default is true//$v->length= "4";//verification code points $v->usezh = True;//chinese code $v->fontttf= "stkaiti. TTF "; $v->entry ();//generate verification code}public function YZ () {$yzm = $_post[" yzm "]; $v = new \think\verify (); if ($v->check ($yzm)) {//verify $this->ajaxreturn ("ok", "eval");} Else{$this->ajaxreturn ("no", "eval");}}          }    

The effect of the implementation is as Follows:

Click Verify after entering Text:

TWO. Two verification codes

1. Add an IMG tag to the template

<body>

2. Controller method also write a yzm method, called yzm2, if there are two verification code, entry will be identified, entry (1) entry (2)

Public function Yanzheng () {$this->show ();//display template page}public function Yzm () {$v = new \think\verify ();//object//$v USEIMGBG = True;//whether The background picture is used by default is false//$v->fontsize= "30";//captcha font size (pixels) default to 25//$v->usecurve=false;//use confusion curve The default is true//$v->usenoise=false;//whether to add a noise default is true//$v->length= "4";//verification code points $v->usezh = True;//chinese code $v-> fontttf= "stkaiti. TTF "; $v->entry (1);//generate verification code}public function YZ () {$yzm = $_post[" yzm "]; $v = new \think\verify (); if ($v->check ($yzm, 1) {//verify $this->ajaxreturn ("ok", "eval");} Else{$this->ajaxreturn ("no", "eval");}} Public Function yzm2 () {$v = new \think\verify (); $v->entry (2);//generate Verification code}

3. Then write an identity in the check method, which is the identity to verify which is the first

Public Function yz () {$yzm = $_post["yzm"]; $v = new \think\verify (); if ($v->check ($yzm, 1)) {//verify $this->ajaxreturn ( "ok", "eval");} Else{$this->ajaxreturn ("no", "eval");}}

The results displayed:

Note: Sometimes the browser will have a bug, such as IE does not refresh, so we want to add a thing, let it adapt to any browser, can write

$ ("#img"). Click (function () {var SJS = Math.floor (math.random () *100);//generate random number $ (this). attr ("src", "__controller__/yzm/ Code/"+sjs);//stitching on the truncated random Number})

Three. Verification Code parameters

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 ();

emphasis on the method of Chinese verification Code 1. There is no font parameter in the frame, so we find the font parameter from C Disk.

2. Select the font parameters in the TTF format to copy to the Thinkphp\thinkphp\library\think\verify\zhttfs folder

3. Call the Chinese captcha parameter in the code

Public function Yzm () {    $v = new \think\verify ();//build object    $v->usezh = True;//chinese Verification Code    $v->fontttf= "stkait  I.ttf ";    $v->entry (1);//generate Verification code}

thinkphp Framework: Verification Code function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.