在laravel框架中如何使用验证码
以下为详细的步骤
执行命令
```
composer require gregwar/captcha
```
创建控制器
```
use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;
use Cache;
use Illuminate\Support\Facades\Session;
public function index(Request $request)
{
$builder = new CaptchaBuilder();
$builder->build(150,47);
// 设置背景颜色
$builder->setBackgroundColor(220, 210, 230);
$builder->setMaxAngle(25);
$builder->setMaxBehindLines(0);
$builder->setMaxFrontLines(0);
//可以设置图片宽高及字体
$builder->build($width = 100, $height = 50, $font = null);
//获取验证码内容
$phrase = $builder->getPhrase();
//把内容存入session 存储验证码
$request->session()->flash('captchaSession', $phrase);
//清除缓存
ob_clean();
//把验证码数据以jpeg图片的格式输出
return response($builder->output())->header('Content-type','image/jpeg');
}
//判断验证码
public function check(Request $request)
{
$code = $request->input("yzm"); //验证码
// echo "code->".$code;
// echo Session::get('phrase');
// echo "
"; // print_r($request->session()); // echo "
";
if (strtolower(Session::get('captchaSession')) ==strtolower($code)) {
Session::forget('phrase');
return '验证码正确';
} else {
//用户输入验证码错误
return $this->jsonData('验证码错误');
}
```