zoukankan      html  css  js  c++  java
  • laravel 使用验证码

    
    

    laravel 拥有composer这个包管理工具 使用相关依赖就变得容易的多 

    使用验证码,首先php要开启gd库

    这次我们使用扩展库是gregwar/captcha

    1.在根目录coposer.json添加如下

    "require": {  
            ...  
            "gregwar/captcha": "1.*"  
        },  
    

    2.使用composer 更新

    composer update
    

    3.调用库中的方法

    <?php
    
    namespace AppHttpControllersBackend;
    
    use IlluminateHttpRequest;
    
    use AppHttpRequests;
    use AppHttpControllersController;
    use GregwarCaptchaCaptchaBuilder;
    use Session;
    
    class BackController extends Controller
    {
      /**
         * 验证码生成
         * @param  [type] $tmp [description]
         * @return [type]      [description]
         */
        public function captcha($tmp)
        {
            //生成验证码图片的Builder对象,配置相应属性
            $builder = new CaptchaBuilder;
            //可以设置图片宽高及字体
            $builder->build($width = 100, $height = 40, $font = null);
            //获取验证码的内容
            $phrase = $builder->getPhrase();
            //把内容存入session
            Session::flash('milkcaptcha', $phrase);
            //生成图片
            header("Cache-Control: no-cache, must-revalidate");
            header('Content-Type: image/jpeg');
            $builder->output();
        }  
    }
    

    4.注册路由

    //生成验证码
    Route::get('login/captcha/{tmp}', 'BackController@captcha');
    

    5.view中使用

    <img  src="{{ url('login/captcha/1') }}"  alt="验证码" title="刷新图片" width="100" height="40" id="captcha_img" border="0">

    6.后台验证

          $code = $request->input('code');
    
    
          if(Session::get('milkcaptcha')!=$code) {
            return redirect('/')->with('message','验证码错误');
          }
    

      

  • 相关阅读:
    客户端命令行查看dubbo服务的生产者和消费者
    灰度发布
    灰度发布方法了解
    spring事务隔离级别
    小东西
    从excel表中生成批量SQL,将数据录入到数据库中
    CSV导出
    详解Java的自动装箱与拆箱(Autoboxing and unboxing)
    java多线程理解
    spring事务传播行为讲解转载
  • 原文地址:https://www.cnblogs.com/zhoupufelix/p/7794129.html
Copyright © 2011-2022 走看看