Home  >  Article  >  PHP Framework  >  How to change thinkphp default verification code

How to change thinkphp default verification code

PHPz
PHPzOriginal
2023-04-13 18:34:161047browse

thinkphp is a popular PHP framework for developing web applications. When using the thinkphp framework, verification codes are often needed to increase system security. The default verification code styles and images of thinkphp may not be suitable for certain application scenarios and need to be changed. This article will introduce how to change the default verification code of thinkphp.

1. Find the verification code controller

In the thinkphp framework, the verification code is generated and output by a built-in controller. The source code of this controller can be found in the library file directory of the thinkphp framework. The general path is: thinkphp/library/think/captcha/Controller.php.

2. Copy the verification code controller

In order to avoid errors during system updates caused by modifying the default verification code controller, you should copy the default verification code controller to a new location in the application directory. in the directory.

The specific steps are as follows:

  1. Create a new directory, for example: \application\extra\captcha
  2. Copy the controller Controller.php to this directory.
  3. Modify the namespace of Controller.php to: namespace app\extra\captcha;

3. Modify the verification code controller

Now we will You can modify the verification code controller to change the generation and output methods of verification codes.

  1. Modify the output method

The default output method of the verification code is to return a picture in gif format, and we can modify the output method to implement other styles of verification codes. For example, the verification code can be output as an image in SVG format.

Modify the verification code output method as follows:

public function show()
{
    $config =    [
        'expire'   =>  3,   //验证码过期时间(s)
        'length'   =>  4,   //验证码长度
        'imageH'   =>  60,   //验证码高度
        'fontSize' =>  20,  //验证码字体大小(px)
        'useCurve' =>  false,  //是否画混淆曲线
        'useNoise' =>  false,  //是否添加杂点
        'bg'       =>  [255, 255, 255], //背景颜色
    ];
    $captcha = new Captcha($config);
    $captcha->codeSet = '0123456789';//只用数字作为验证码
  
    // 输出svg格式的图片
    echo $captcha->entry('svg'); 
}
  1. Modify the verification code generation method

thinkphp’s default verification code generation method is to randomly generate numbers and letters , and we can also modify the generation method to meet different needs. For example, a verification code in Chinese characters can be generated.

Modify the verification code generation method as follows (taking Chinese verification code generation as an example):

public function show()
{
    //引入新字体文件(这里以方正胖头鱼体为例,字体文件需要自己上传)
    $ttf_file = __DIR__ .'/fzpty.ttf';
    $codeSet = '玩家参与游戏,从而培养了自己 的游戏能力'; //验证码文字内容
  
    $config =    [
        'expire'  =>  3,   //验证码过期时间(s)
        'length'  =>  4,   //验证码长度
        'useZh'   => true, //使用中文验证码
        'fontttf' =>  $ttf_file, //指定ttf字体文件
        'imageH'  =>  60,   //验证码高度
        'fontSize' =>  20,  //验证码字体大小(px)
        'useCurve' =>  false,  //是否画混淆曲线
        'useNoise' =>  false,  //是否添加杂点
        'bg'       =>  [255, 255, 255], //背景颜色
    ];
    $captcha = new Captcha($config);
    $captcha->codeSet = $codeSet;//设置验证码文字内容
  
    // 输出svg格式的图片
    echo $captcha->entry('svg'); 
}

4. Modify the verification code configuration file

thinkphp verification code controller will read The parameters in the configuration file determine how the verification code is generated and output. Therefore, we can also modify the configuration file to achieve the purpose of changing the verification code.

The specific steps are as follows:

  1. Copy the framework's default verification code configuration file to your own application directory

Open the thinkphp framework's verification code configuration file config. php, copy it to the config directory under the application directory, and rename it to captcha.php.

  1. Modify the verification code configuration

You can modify the related configuration parameters of the verification code in captcha.php. For example, you can modify the length, expiration time, font size and other parameters of the verification code:

return [
    'useNoise'    => false, // 是否添加杂点
    'useCurve'    => false, // 是否画混淆曲线
    'fontSize'    => 25,    // 验证码字体大小
    'expire'      => 1800,  // 验证码过期时间(s)
    'length'      => 4,     // 验证码长度
    'fontttf'     => '',    //验证码字体文件路径
    'bg'          => [255, 255, 255], // 验证码背景颜色,如果设置为数组则表示渐变色
];

5. Summary

Through the above steps, we can modify thinkphp’s default verification code to achieve customization Defined styles and verification code content. It should be noted that when modifying the verification code controller and configuration files, the copied files must be placed in the application directory to avoid overwriting after version updates.

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

Statement:
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