Home > PHP Framework > ThinkPHP > Discuss the implementation of thinkphp verification code

Discuss the implementation of thinkphp verification code

PHPz
Release: 2023-04-17 11:06:27
Original
714 people have browsed it

With the development of Internet technology, the application of verification code is becoming more and more widespread. It can effectively prevent malicious registration, comments, crawlers and other behaviors of robots, ensuring the security and normal operation of the website. In order to better realize the function of verification code , now let's discuss the implementation of thinkphp verification code.

1. The concept and function of verification code

Verification code (full English name: Completely Automated Public Turing test to tell Computers and Humans Apart) is a public test that automatically distinguishes computer programs from human users. The fully automatic reverse Turing test is a technology used to distinguish whether the user is a machine or a human. Verification codes are widely used, including but not limited to registration, login, comment, search, voting, crawler and other scenarios, and play a vital role in the security of the Internet.

2. Implementation steps of thinkphp verification code

  1. Install thinkcaptcha

First, we need to install the thinkcaptcha expansion package in thinkphp, you can use the following command Installation: composer require topthink/think-captcha

  1. Configure the config.php file

In the application folder, find the config.php file. After opening it, you can see the following Code:

//验证码
'captcha' => [
    // 验证码字符集合
    'codeSet' => '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY',
    // 验证码字体大小(px),根据所需进行设置验证码字体大小
    'fontSize' => 25,
    // 是否画混淆曲线
    'useCurve' => true,
    // 验证码图片高度,根据所需进行设置验证码图片高度
    'imageH' => 50,
    // 验证码图片宽度,根据所需进行设置验证码图片宽度
    'imageW' => 150,
    // 验证码位数,根据所需进行设置验证码位数
    'length' => 4,
    // 验证成功后是否重置
    'reset' => true
]
Copy after login

According to needs, we can modify the parameters for personalized settings.

  1. Call the verification code method in the controller
use think\captcha\Captcha;

class Index
{
   public function verify()
   {
      $config = [
         'codeSet' => '0123456789',
         'useZh'   => false,
         'fontSize'=> 20, 
         'length' => 4,
         'useNoise' => false,
      ]; //验证码配置
      $captcha = new Captcha($config);
      return $captcha->entry(); 
   }
}
Copy after login

In the above content, $config is used to set the verification code parameters, such as codeSet is an optional character set, useZh is whether to enable Chinese, fontSize is the font size of the verification code, length is the number of digits of the verification code, useNoise is whether to enable interference lines, etc.,

  1. Call the verification code in the view

In the form that requires the use of verification code, you can call the verification code through the following code:

<form method="post">
   <input type="text" name="captcha" placeholder="Captcha" class="form-control">
   <?php echo captcha_img(); ?>
</form>
Copy after login

The captcha_img() method is the verification code output method that comes with the thinkcaptcha expansion package, which can be used to directly output the generated Verification code.

  1. Verify verification code

Finally, the verification code needs to be verified on the backend. If the verification code entered by the user is inconsistent with the generated verification code, the verification will fail.

use think\captcha\Captcha;

class Index
{
   public function login()
   {
      $captcha = new Captcha();
      if (!$captcha->check(input('code'))) {
         $this->error('验证码错误');
      } else {
         //其他业务逻辑
      }
   }
}
Copy after login

In the above code, $captcha->check(input('code')) is used to detect whether the verification code entered by the user is consistent with the generated verification code. If it is inconsistent, a prompt will pop up.

3. Summary

Because verification code is an important part of ensuring website security, thinkphp also provides rich verification code functions. In the process of implementing verification code, you need to pay attention to the configuration parameters and When calling the method, you also need to combine the implementation of the verification code with the business logic, and skillfully use the advantages of the verification code to protect the security and healthy development of the website.

The above is the detailed content of Discuss the implementation of thinkphp verification code. For more information, please follow other related articles on the PHP Chinese website!

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