Home > PHP Framework > ThinkPHP > body text

How to use verification code in ThinkPHP6

王林
Release: 2023-06-21 10:05:26
Original
1899 people have browsed it

With the increasing emphasis on network security, verification codes are widely used as a common verification method. In web applications, verification codes can effectively prevent malicious attacks and automated robot operations, ensuring the security and reliability of information. ThinkPHP6, as an excellent PHP framework, also provides support for the verification code function. This article will introduce how to use verification codes in ThinkPHP6.

  1. Install verification code extension
    ThinkPHP6 does not include verification code extension by default and needs to be installed manually. It can be installed through composer. The command is as follows:

    composer require topthink/think-captcha
    Copy after login
  2. Configure verification code
    In ThinkPHP6, the configuration of verification code needs to be done in the captcha.php file in the config directory. This file does not exist by default. You need to manually create and add the following configuration information:

    <?php
    
    return [
     // 验证码位数
     'length'    => 4,
     // 验证码字符集合
     'codeSet'   => '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY',
     // 验证码过期时间(s)
     'expire'    => 1800,
     // 验证码字体大小(px)
     'fontSize'  => 25,
     // 是否画混淆曲线
     'useCurve'  => true,
     // 是否添加杂点
     'useNoise'  => true,
     // 验证码图片高度
     'imageH'    => 60,
     // 验证码图片宽度
     'imageW'    => 220,
     // 验证码背景颜色(rgb数组,设置为null随机颜色)
     'bg'        => [243, 251, 254],
     // 验证码字体颜色(rgb数组,设置为null随机颜色)
     'fontColor' => null,
    ];
    Copy after login

The above are some commonly used verification code configuration items, which can be modified according to actual needs. The function of each configuration item has corresponding comments.

  1. Generate verification code
    When generating a verification code, you first need to instantiate the verification code tool class. Just use the following code in the controller:

    use thinkcaptchaacadeCaptcha;
    
    class Demo extends Controller
    {
     // 生成验证码
     public function captcha()
     {
         return Captcha::create();
     }
    }
    Copy after login

The parameter of the create() method can be a number, indicating the number of verification code characters; it can also be a number containing multiple options array, see the following code for specific instructions:

$options = [
    'length'    => 4,
    'fontSize'  => 25,
    'imageW'    => 220,
    'imageH'    => 60,
    'useCurve'  => false,
    'useNoise'  => true,
    'reset'     => true,
    'fontttf'   => '',
    'bg'        => [243, 251, 254],
    'expire'    => 1800,
    'codeSet'   => '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY',
    'zh'        => false,
    'math'      => false,
    'addChars'  => '',
    'fontColor' => null,
];
return Captcha::create($options);
Copy after login
  1. Verification verification code
    When the user submits the form, the verification code needs to be verified. This can be achieved through the following code:

    use thinkcaptchaacadeCaptcha;
    
    class Demo extends Controller
    {
     // 验证验证码
     public function checkCaptcha($captcha)
     {
         if (Captcha::check($captcha)) {
             // 验证码正确
             return true;
         } else {
             // 验证码错误
             return false;
         }
     }
    }
    Copy after login

The parameter $captcha is the verification code string entered by the user. The Captcha::check($captcha) function will automatically compare it with the one saved in the session. The verification codes are compared, and if they are equal, true is returned, otherwise false is returned.

So far, we have completed the use of verification code in ThinkPHP6. Through the four steps of installing the extension, configuring the verification code, generating the verification code, and verifying the verification code, we can easily implement the verification code function and improve the security and reliability of web applications.

The above is the detailed content of How to use verification code in ThinkPHP6. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template