thinkphp Dynamic Verification Code

Source: Internet
Author: User

<?PHPnamespaceHome\controller;use Think\controller;classTestController extends controller{ Publicfunction Test () {$y=d ("Yonghu"); $arr=Array (Array ('UID','require','the user name cannot be empty! '),//form validation); if($y->validate ($arr)->create ())//use validate to add rules to determine success{$verify=New\think\verify (); $a= $verify->check ($_post["Yzm"]);//How to verify the verification code                      if($a)//verify by returning True{echo" One";//Validation Successful$yAdd (); $ This->ajaxreturn ("by verifying","Eval"); }           Else{echo" A";//validation Failed           }       }       Else       {           $ This->ajaxreturn ($y->geterror (),"Eval");//Form validation error message       }               }




Public Function Yzm ()
{
$V = new \think\verify (); Method of generating dynamic graph verification code
$V->entry ();
}







Verification Code Previous page next page

The Think\verify class can support the generation and validation of verification codes.

Generate Verification Code

Here is the simplest way to generate a verification code:

    1. $Verify = new \Think\Verify();
    2. $Verify->entry();

The code above generates a default CAPTCHA image and outputs it, as shown below:

The generated verification code information is saved to the session and contains the following data:

    1. array(‘verify_code‘=>‘当前验证码的值‘,‘verify_time‘=>‘验证码生成的时间戳‘)

If you need to generate multiple verification codes in a single page, the entry method needs to pass in the identifiable information, for example: Code 1:

    1. // 验证码1
    2. $Verify = new \Think\Verify();
    3. $Verify->entry(1);

Verification Code 2:

    1. // 验证码2
    2. $Verify = new \Think\Verify();
    3. $Verify->entry(2);
Verification Code Parameters

You can set the relevant parameters for the generated captcha to achieve different display results. These parameters include:

Parameters Description
Expire Validity period of the verification code (in seconds)
Useimgbg Default to False if using background picture
FontSize Captcha font size (pixels) defaults to 25
Usecurve Whether to use the obfuscation curve by default to True
Usenoise Whether to add a noise by default to True
Imagew Verify that the code width is set to 0 for automatic calculation
Imageh Verify that the code height is set to 0 for automatic calculation
Length Verify number of code bits
Fontttf Specifies that the CAPTCHA font is randomly fetched by default
Usezh Whether to use Chinese verification code
Bg Verification code background color RGB array settings, e.g. array (243, 251, 254)
Sekey Encryption key for verification code
CodeSet Verification code character Set 3.2.1 new
Zhset Verification Code Character set (Chinese) 3.2.1 New

The parameter settings are used in two ways.

Instantiate incoming parameters:

  1. $config = array(
  2. ‘fontSize‘ => 30, // 验证码字体大小
  3. ‘length‘ => 3, // 验证码位数
  4. ‘useNoise‘ => false, // 关闭验证码杂点
  5. );
  6. $Verify = new \Think\Verify($config);
  7. $Verify->entry();

Or in a dynamically set-up way, such as:

  1. $Verify = new \Think\Verify();
  2. $Verify->fontSize = 30;
  3. $Verify->length = 3;
  4. $Verify->useNoise = false;
  5. $Verify->entry();

Generated Verification Code:

Verification Code Font

By default, the font of the captcha is random using ThinkPHP/Library/Think/Verify/ttfs/ the font file under the directory, and we can specify the font of the CAPTCHA, for example:

    1. $Verify = new \Think\Verify();
    2. // 验证码字体使用 ThinkPHP/Library/Think/Verify/ttfs/5.ttf
    3. $Verify->fontttf = ‘5.ttf‘;
    4. $Verify->entry();
Background image

Support Verification Code background image function, can be set as follows:

    1. $Verify = new \Think\Verify();
    2. // 开启验证码背景图片功能 随机使用 ThinkPHP/Library/Think/Verify/bgs 目录下面的图片
    3. $Verify->useImgBg = true;
    4. $Verify->entry();

Effect:

Chinese Verification Code

If you want to use a Chinese verification code, you can set:

    1. $Verify = new \Think\Verify();
    2. // 验证码字体使用 ThinkPHP/Library/Think/Verify/ttfs/5.ttf
    3. $Verify->useZh = true;
    4. $Verify->entry();

Show effect

If this does not work, make sure that the Chinese font file exists under your thinkphp/library/think/verify/zhttfs/directory.

Specify the CAPTCHA character

3.2.1 versions above, we can specify the character of the captcha by re-setting the codeset parameter, for example:

    1. $Verify = new \Think\Verify();
    2. // 设置验证码字符为纯数字
    3. $Verify->codeSet = ‘0123456789‘;
    4. $Verify->entry();

If it is a Chinese verification code, you can use the zhset parameter settings, for example:

    1. $Verify = new \Think\Verify();
    2. $Verify->useZh = true;
    3. // 设置验证码字符
    4. $Verify->zhSet = ‘们以我到他会作时要动国产的一是工就年阶义发成部民可出能方进在了不和有大这‘;
    5. $Verify->entry();
Verification Code Detection

The

can use the check method of the Think\verify class to detect that the input of the verification code is correct, for example, the following is a function of the encapsulated verification Code detection:

    1. //Detect input Verification code is correct, $code for user input captcha string
    2. Span class= "KWD" >function Check_verify ( $code , $id = " {
    3. $verify = new \think\verify< Span class= "pun" > ()
    4. return $verify - >check ( $code , $id Span class= "pun");
    5. }
Previous page Next page

thinkphp Dynamic Verification Code

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.