一、在框架根目录下下载图形验证码的组件
composer require gregwar/captcha=1.*
二、控制器代码
use GregwarCaptchaCaptchaBuilder;//使用 public function admin_log()//生成验证码且展示的方法 { $builder = new CaptchaBuilder; $builder->build(); $code = $builder->inline(); //获取图形验证码的url session()->put('piccode', $builder->getPhrase()); //将图形验证码的值写入到session中 return view('Bigone/admin_log',['code'=>$code]); }
三、视图代码
<tr>
<td>验证码</td>
<td><input name="captcha" type="text" placeholder="请输入验证码"></td>
<td><img src="{{$code}}" height="36" width="80"></td>
</tr>
四、验证
public function admin_login(Request $request) { $data = $request->all(); $captcha = $data['captcha']; $code = session()->get('piccode'); if($captcha == '') { echo "<script>alert('请输入验证码!');</script>"; return $this->admin_log(); exit(); } elseif($captcha!=$code) { echo "<script>alert('验证码错误!');</script>"; return $this->admin_log(); exit(); } else { $user_pass = md5($data['user_pass']); $res = DB::select("select * from pzq_sysuser where user_name = '{$data['user_name']}' and user_pass = '$user_pass'"); if($res) { echo "<script>alert('登陆成功!');</script>"; return $this->index_show(); } else { echo "<script>alert('登陆失败,请检查用户名和密码!');</script>"; return $this->admin_log(); } } }