<?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:
$Verify = new \Think\Verify();
$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:
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
$Verify = new \Think\Verify();
$Verify->entry(1);
Verification Code 2:
// 验证码2
$Verify = new \Think\Verify();
$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:
$config = array(
‘fontSize‘ => 30, // 验证码字体大小
‘length‘ => 3, // 验证码位数
‘useNoise‘ => false, // 关闭验证码杂点
);
$Verify = new \Think\Verify($config);
$Verify->entry();
Or in a dynamically set-up way, such as:
$Verify = new \Think\Verify();
$Verify->fontSize = 30;
$Verify->length = 3;
$Verify->useNoise = false;
$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:
$Verify = new \Think\Verify();
// 验证码字体使用 ThinkPHP/Library/Think/Verify/ttfs/5.ttf
$Verify->fontttf = ‘5.ttf‘;
$Verify->entry();
Background image
Support Verification Code background image function, can be set as follows:
$Verify = new \Think\Verify();
// 开启验证码背景图片功能 随机使用 ThinkPHP/Library/Think/Verify/bgs 目录下面的图片
$Verify->useImgBg = true;
$Verify->entry();
Effect:
Chinese Verification Code
If you want to use a Chinese verification code, you can set:
$Verify = new \Think\Verify();
// 验证码字体使用 ThinkPHP/Library/Think/Verify/ttfs/5.ttf
$Verify->useZh = true;
$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:
$Verify = new \Think\Verify();
// 设置验证码字符为纯数字
$Verify->codeSet = ‘0123456789‘;
$Verify->entry();
If it is a Chinese verification code, you can use the zhset parameter settings, for example:
$Verify = new \Think\Verify();
$Verify->useZh = true;
// 设置验证码字符
$Verify->zhSet = ‘们以我到他会作时要动国产的一是工就年阶义发成部民可出能方进在了不和有大这‘;
$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:
-
//Detect input Verification code is correct, $code for user input captcha string
-
Span class= "KWD" >function Check_verify ( $code , $id = " {
-
$verify = new \think\verify< Span class= "pun" > ()
-
return $verify - >check ( $code , $id Span class= "pun");
-
}
Previous page Next page
thinkphp Dynamic Verification Code