thinkphp5.0.24 verification code

Here use composer to install the verification code class.
Check whether there is topthink under the vendor:
if think-captcha is not available, enter the CMD command line interface and use composer to download:

In order to increase the speed, it is recommended to use a domestic mirror site.

composer config -g repo.packagist composer https://packagist.phpcomposer.com

cd to the web directory and execute the following command:
tp5.0 uses the expansion package under 1.0, here is version 1.0.8

composer require topthink/think-captcha=1.0.8

Then enter the configuration file to configure the rules of the verification code

//验证码
    'captcha' =>[
        //验证码的字符集
        'codeSet' => '123456798',
        //设置字体大小
        'fontSize' => 18,
        //添加混淆曲线
        'useCurve' => true,
        //设置图片宽高
        'imagew' => 150,
        'imageH' => 35,
        //位数
        'length' => 4,
        //验证成功重置
        'reset' => true,
    ],

In vendor\topthink\think-captcha\src\helper.php; find the captcha_img() method and
replace the contents with

$js_src = "this.src='".captcha_src()."'";
    return '<img src="' . captcha_src($id) . '" title="点击更新验证码" alt="点击更新验证码" onclick="'.$js_src.'" />';

Controller write method

public function index()
{
    return $this->fetch();
}
//验证
public function captcha(){
    	if(request()->isPost()){
            $data = input('post.');
            if(!captcha_check($data['verifyCode'])) {
                // 校验失败
                $this->error('验证码不正确');
            }else{
            	$this->success('验证码正确');
            }
		}
    }

index view

<form method="post" action="{:url('captcha')}">
	<input type="text" name="verifyCode" class="layui-input">
	<!-- <img src="{:captcha_src()}"> -->
    <div>{:captcha_img()}</div>
    <button type="submit">提交</button>
</form>

Tip: If the php version is 7 or higher, it may not be displayed. You
need to remove extension=php_gd2.dll in the configuration file php.ini, remove the semicolon in front, and add this sentence if not.

Guess you like

Origin blog.csdn.net/hgb24660/article/details/100053514