1)php.ini需要开两个扩展
extension=php_fileinfo.dll
extension=php_gd2.dll
2)使用composer安装类包
composer require mews/captcha
3)配置文件里添加服务及服务别名(门面)
'providers' => [ // ... MewsCaptchaCaptchaServiceProvider::class, ] 'aliases' => [ // ... 'Captcha' => MewsCaptchaFacadesCaptcha::class, ]
4)生成验证图片
1、直接生成图片
captcha();
2、返回验证码图片url
captcha_src();
3、返回html(img标签)
captcha_img();
5)验证码验证
$rules = ['captcha' => 'required|captcha']; $validator = Validator::make(Input::all(), $rules); if ($validator->fails()) { echo '<p style="color: #ff0000;">Incorrect!</p>'; } else { echo '<p style="color: #00ff30;">Matched :)</p>'; }