Home > PHP Framework > ThinkPHP > body text

How to install the ThinkPHP verification code plug-in

WBOY
Release: 2023-06-02 21:08:37
forward
1157 people have browsed it

We first need to open the ThinkPHP official website and search for content related to the verification code. We can find some documents introducing verification codes and already developed verification code plug-ins in the search results. This article will introduce two verification code integration methods: using the officially provided verification code plug-in and manually writing code.

1. Use the official verification code plug-in

In the official documentation, we can find how to use the ThinkPHP verification code plug-in. To use the official plug-in, you need to perform the following steps:

1.1 Create a new Verify folder in the extend directory of the ThinkPHP framework and put the downloaded verification code plug-in into it.

1.2 View the ThinkPHP configuration file and point the verification code configuration item to the folder where the verification code plug-in was just placed. The specific code is as follows:

'verify' =>[
    //使用中文验证码
    'useZh'=>false,
    //验证码字体大小(px)
    'fontSize'=>25,
    //验证码位数
    'length'=>5,
    //验证码图片宽度(像素)
    'imageW'=>0,
    //验证码图片高度(像素)
    'imageH'=>0,
    //关闭验证码杂点 
    'useNoise'=>true,
    //背景颜色(16进制色值)
    'bg'=>[243, 251, 254],
    //需要包含的字符集合
    'codeSet'=>'0123456789',
    //验证码字符间隔(px)
    'seKey'=>"ThinkPHP.CN_",//密钥
    ...
],
Copy after login

It should be noted that the parameters imageW and imageH can be set according to the actual situation. If not set, the size of the verification code image will be the same as the size of the output image by default.

1.3 Wherever the verification code needs to be output, use the following code to integrate the official verification code plug-in:

$img = ( new \Think\Verify())->entry();  
echo $img;
Copy after login

2. Manually write the verification code generation code

In addition to using the official plug-in, we can also manually write the verification code generation code. The specific process is as follows:

First, we need to create a verification code class, which contains methods for generating and outputting verification codes. The following code is an important part of the hand-coded verification code class:

class VerifyCode
{
    //验证码字符长度
    private $length = 4;

    //验证码字符数组
    private $codes = [];

    //验证码生成
    public function generate()
    {   
        //生成字符数组
        $this->codes = [];
        for($i = 0; $i < $this->length; ++$i) {
            $this->codes[] = chr(mt_rand(48, 57));
        }

        //保存字符数组到session中
        session(&#39;verifycode&#39;, implode(&#39;&#39;, $this->codes));

        //开启输出缓存
        ob_start();
        header(&#39;Content-Type:/image/png&#39;);

        //创建验证码图片
        $image = imagecreate(100, 40);

        //设置画布背景颜色 
        $bg_color = imagecolorallocate($image, 238, 238, 238); 
        imagefill($image, 0, 0, $bg_color);

        //绘制验证码字符
        for($i = 0; $i < $this->length; ++$i) {
            $font_file = &#39;/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf&#39;;
            $text_color = imagecolorallocate(
                $image, mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150));
            imagettftext($image, 24, mt_rand(-20, 20), 5 + $i * 25, 30, 
                         $text_color, $font_file, $this->codes[$i]);
        }

        //输出验证码图片
        imagepng($image);
        imagedestroy($image);
        $img = ob_get_contents();
        ob_end_clean();

        return $img;
    }
}
Copy after login

2.2 Use the following code to generate and output the verification code where the verification code is required:

$vf = new VerifyCode();
echo $vf->generate();
Copy after login

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

Related labels:
source:yisu.com
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