Think \ Verify class can support the generation and verification of functional verification code.
Generating codes
Here is the easiest way to generate codes:
$Verify = new \Think\Verify();
$Verify->entry();
The above code will generate a default image and outputting the codes, is shown below:
Generating a verification code information is stored in the session, the data included are:
array('verify_code'=>'当前验证码的值','verify_time'=>'验证码生成的时间戳')广州大理石平台
If you need to generate a plurality of codes in a page, then, need to pass entry method identification information may be, for example: Code 1:
// 验证码1
$Verify = new \Think\Verify();
$Verify->entry(1);
Code 2:
// 验证码2
$Verify = new \Think\Verify();
$Verify->entry(2);
Code parameters
Set the relevant parameters for a code, in order to achieve various display effects. These parameters include:
parameter | description |
---|---|
expire | Valid verification code (s) |
useImgBg | Whether to use the default background image is false |
fontSize | Code font size (pixels) defaults to 25 |
useCurve | Whether to use the confusion curve defaults to true |
useNoise | Adding noise if the default is true |
imageW | Codes width is set to 0 to automatically calculate |
imageH | Codes height calculated automatically set to 0 |
length | Code-digit |
fontttf | Specifies the default font for the random verification code acquisition |
useZh | Whether Chinese code |
bg | BACKGROUND rgb color codes arrays disposed such array (243, 251, 254) |
seKey | Encryption key verification codes |
codeSet | 3.2.1 new code set of characters |
zhSet | Code set of characters (Chinese) New 3.2.1 |
Parameter settings in two ways.
Examples of passing parameters:
$config = array(
'fontSize' => 30, // 验证码字体大小
'length' => 3, // 验证码位数
'useNoise' => false, // 关闭验证码杂点
);
$Verify = new \Think\Verify($config);
$Verify->entry();
Or by way of dynamically set, such as:
$Verify = new \Think\Verify();
$Verify->fontSize = 30;
$Verify->length = 3;
$Verify->useNoise = false;
$Verify->entry();
The codes generated as shown:
Code font
By default, this code is random use of fonts ThinkPHP/Library/Think/Verify/ttfs/
directory of font files, we can specify font code verification, such as:
$Verify = new \Think\Verify();
// 验证码字体使用 ThinkPHP/Library/Think/Verify/ttfs/5.ttf
$Verify->fontttf = '5.ttf';
$Verify->entry();
Background picture
Support Code Background images can be set as follows:
$Verify = new \Think\Verify();
// 开启验证码背景图片功能 随机使用 ThinkPHP/Library/Think/Verify/bgs 目录下面的图片
$Verify->useImgBg = true;
$Verify->entry();
Results as shown:
Chinese code
If you want to use the Chinese code, you can set:
$Verify = new \Think\Verify();
// 验证码字体使用 ThinkPHP/Library/Think/Verify/ttfs/5.ttf
$Verify->useZh = true;
$Verify->entry();
FIG display:
If you can not display correctly, confirm the presence of Chinese font file your ThinkPHP / Library / Think / Verify / zhttfs / directory.
Specifies the character code
3.2.1 version of the above, we can verify the character code specified by resetting codeSet parameters. For example:
$Verify = new \Think\Verify();
// 设置验证码字符为纯数字
$Verify->codeSet = '0123456789';
$Verify->entry();
If Chinese codes, may be used zhSet parameter settings, for example:
$Verify = new \Think\Verify();
$Verify->useZh = true;
// 设置验证码字符
$Verify->zhSet = '们以我到他会作时要动国产的一是工就年阶义发成部民可出能方进在了不和有大这';
$Verify->entry();
Detecting codes
Can Think \ Verify type check
input method for detecting a verification code is correct, for example, the following is a function of the package validation code detection:
// 检测输入的验证码是否正确,$code为用户输入的验证码字符串
function check_verify($code, $id = ''){
$verify = new \Think\Verify();
return $verify->check($code, $id);