NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
check 下面来进行验证码检测,在控制器中添加验证方法 <?php namespace app\\index\\controller; class Captcha extends \\think\\Controller { //验证码表单 public function index() { return $this->fetch(); } //验证码检测 public function check($code='') { $captcha = new \\think\\captcha\\Captcha(); if (!$captcha->check($code)) { $this->error('验证码错误'); } else { $this->success('验证码正确'); 的路由地址,因此请确保你已经开启URL路由。 ,代码如下: } } } 当我们输入一个错误的验证码后,页面提示: ![Image](https://box.kancloud.cn/6cb1ae9ff4c9ec9ed34213b7e3d14dab_320x320.jpeg) 如果输入正确,则显示: ![Image](https://box.kancloud.cn/0d843b06924ad767b34b9a9bc2bccd03_321x313.jpeg) 扩展包内置提供了助手函数 <?php namespace app\\index\\controller; class Captcha extends \\think\\Controller captcha\_check check 用来检测验证码,所以 方法可以简化为: { //验证码表单 public function index() { return $this->fetch(); } //验证码检测 public function check($code) { if (!captcha\_check($code)) { $this->error('验证码错误'); } else { $this->success('验证码正确'); } } } 更简单的用法可以使用控制器的 方法进行验证,例如: validate <?php namespace app\\index\\controller; use think\\Request; class Captcha extends \\think\\Controller { //验证码表单 public function index() { return $this->fetch(); } //验证码检测 public function check(Request $request) { if (true !== $this->validate($request->param(),\[ 'code|验证码'=>'require|captcha' \])) { $this->error('验证码错误'); } else { $this->success('验证码正确'); } } }