Controller
functionYzm () {/*$config = Array (' fontSize ' + 30,//captcha font size ' length ' = 4,//verify Number of yards ' useimgbg ' =>true, ' Usezh ' =>true, ' fontttf ' = ' simyou '. TTF ', ' zhset ' = ' Ah yes, it's a good one to see ');*/ //$v =new \think\verify ($config); Random verification Code of your own definition $v=New\think\verify ();//using the default verification code $v->entry (1);//If you need to generate multiple verification codes in a single page, the entry method needs to pass in the identifiable information, for example: Verification Code 1 } functionXianshi () {if(Empty($_post)) { $this-display (); } Else { $yzm=$_post["Yzm"]; $verify=New\think\verify (); Var_dump($verify->check ($yzm, 1));//1 for the first few verification codes } }View Code
Show Page
<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>Untitled Document</title><Loadhref= "__public__/js/jquery-1.11.2.min.js" /></Head><Body><formAction= "__action__"Method= "POST"><imgID= "Y"src= "__controller__/yzm" /><inputtype= "text"name= "Yzm" /><inputtype= "Submit"value= "Submit" /></form></Body><Scripttype= "Text/javascript">$ (document). Ready (function(e) {$ ("#y"). Click (function(){ varSJ=Math.random (); //get a random number $( This). attr ("src","__controller__/yzm/c"+SJ+""); //for IE browser, each time a different number passed, in order to reload, other browsers do not need })});</Script></HTML>View Code
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 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();
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();
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();
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
You can use the Think\verify class check method to detect the correctness of the input of the verification code, for example, below is a function of the encapsulated verification Code detection:
// 检测输入的验证码是否正确,$code为用户输入的验证码字符串
function check_verify($code, $id = ‘‘){
$verify = new \Think\Verify();
return $verify->check($code, $id);
}
Verification Code of TP frame