thinkphp code

Think \ Verify class can support the generation and verification of functional verification code.

Generating codes

Here is the easiest way to generate codes:

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

The above code will generate a default image and outputting the codes, is shown below:

verify

Generating a verification code information is stored in the session, the data included are:

  1. 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. // 验证码1
  2. $Verify = new \Think\Verify();
  3. $Verify->entry(1);

Code 2:

  1. // 验证码2
  2. $Verify = new \Think\Verify();
  3. $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:

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

Or by way of dynamically set, such as:

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

The codes generated as shown:

localhost

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:

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

Background picture

Support Code Background images can be set as follows:

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

Results as shown:

localhost(2)

Chinese code

If you want to use the Chinese code, you can set:

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

FIG display:

logo

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:

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

If Chinese codes, may be used zhSet parameter settings, for example:

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

Detecting codes

Can Think \ Verify type checkinput method for detecting a verification code is correct, for example, the following is a function of the package validation code detection:

  1. // 检测输入的验证码是否正确,$code为用户输入的验证码字符串
  2. function check_verify($code, $id = ''){
  3. $verify = new \Think\Verify();
  4. return $verify->check($code, $id);

Guess you like

Origin www.cnblogs.com/furuihua/p/11850994.html