一、在框架根目录下下载图形验证码的组件

composer require gregwar/captcha=1.*

二、控制器代码

use Gregwar\Captcha\CaptchaBuilder;//使用

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();
            }
        }
    }

 

相关文章:

  • 2022-12-23
  • 2021-05-30
  • 2022-12-23
  • 2021-08-11
  • 2021-05-30
  • 2022-12-23
  • 2021-11-30
猜你喜欢
  • 2021-11-24
  • 2021-10-05
  • 2021-06-22
  • 2021-05-22
  • 2021-09-04
  • 2022-12-23
  • 2021-05-17
相关资源
相似解决方案