composer 安装 composer require topthink/think-captcha ``` ~~~ 注意命名空间 ~~~ use think\captcha\Captcha; ~~~ //加载验证码 public function code(){ $captcha = new Captcha(); //设置验证码长度 $captcha->length = 4; //验证码宽度 $captcha->imageW= 100; //验证码高度 $captcha->imageH =19; //验证码字体大小 $captcha->fontSize = 10; //设置是否启用验证码是否是中文 // $captcha->useZh = true; // 返回验证码 return $captcha->entry(); } ~~~ ``` ## 模版页面调用 ~~~ <img onclick="this.src='index/user/code?'+Math.random()" alt="点击更换" title="点击更换" src="index/user/code" /> ~~~ ~~~ ## 验证码验证规则 ``` 'code'=>'require|captcha', ``` ~~~ 第二种直接下载;调用 <div style="width: 190px"> <input style="width:50%; float:left;" class="form-control" placeholder="验证码" name="code" type="text"> <img class="form-control" style="width:50%; border:0; padding:0 0 0 5px; float:left; cursor:pointer;" onclick="this.src='{:captcha_src()}?'+Math.random()" src="{:captcha_src()}"> </div>