Home >PHP Framework >ThinkPHP >Detailed introduction to the installation method of ThinkPHP verification code plug-in
In the process of website or application development, verification code is an essential security measure. ThinkPHP, as an excellent PHP development framework, provides developers with a simple verification code integration method. This article will introduce in detail the installation method of ThinkPHP verification code.
First of all, we need to open the official website of ThinkPHP and search for content related to the verification code. From the search results, we can see some verification code documents and already developed verification code plug-ins. In this article, we will use the officially provided verification code plug-in and integrate the verification code by 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_",//密钥 ... ],
It should be noted that the two 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;
After running the code, we can see that the verification code has been successfully integrated:
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:
2.1 First, we need to create a new verification code class and write the verification code generation and output methods in it. 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 length; ++$i) { $this->codes[] = chr(mt_rand(48, 57)); } //保存字符数组到session中 session('verifycode', implode('', $this->codes)); //开启输出缓存 ob_start(); header('Content-Type:/image/png'); //创建验证码图片 $image = imagecreate(100, 40); //设置画布背景颜色 $bg_color = imagecolorallocate($image, 238, 238, 238); imagefill($image, 0, 0, $bg_color); //绘制验证码字符 for($i = 0; $i length; ++$i) { $font_file = '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'; $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; } }
2.2 Use the following code to generate and output verification codes where verification codes are required:
$vf = new VerifyCode(); echo $vf->generate();
The above content is ThinkPHP verification A quick integration method for codes. At present, verification codes have become a very common development security measure. As developers, we need to learn how to integrate it quickly and keep our applications secure.
The above is the detailed content of Detailed introduction to the installation method of ThinkPHP verification code plug-in. For more information, please follow other related articles on the PHP Chinese website!