1)php.ini需要開兩個擴展
extension=php_fileinfo.dll
extension=php_gd2.dll
2)使用composer安裝類包
composer require mews/captcha
3)配置文件里添加服務及服務別名(門面)
'providers' => [ // ... Mews\Captcha\CaptchaServiceProvider::class, ] 'aliases' => [ // ... 'Captcha' => Mews\Captcha\Facades\Captcha::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>'; }