zoukankan      html  css  js  c++  java
  • Lumen5.7快速实现Captcha图片验证码功能

     

    公司发送短信注册的接口需要防刷,需要一个图形验证码,不考虑收费产品。

    Lumen5.7+nginx+mysql

    使用了这个作者的扩展包,只讲实现。https://github.com/Youngyezi/captcha。

    composer require youngyezi/captcha
    注册服务 bootstrapapp.php
    $app->register(YoungyeziCaptchaCaptchaServiceProvider::class);
    // 添加别名
    $app->alias('captcha', 'YoungyeziCaptchaCaptchaServiceProvider');
    复制 vendorYoungyeziCaptchaconfigcaptcha.php 文件至 项目 config 文件下
    Example:生成验证码
    <?php
    use Cache;
    /**
    ** @return array
    * {"sensitive": false,
          "key":"$2y$10$5QrxhlFmuJJgtcA2WpFVSuSTbaf8JVELNSz/cS9oGAS3Skh8cGZf2",
            "img": "data:image/png;base64,iVBORw0II="
        }
    */
     public function captchaInfo()
        {
            $result = app('captcha')->create();
             //这个key可以自定义,我是放到了文件缓存中
            $key = sprintf(config('constants.cache.captcha_code'), $result['key']);
            Cache::set($key,$result['key'],config('constants.cache.ten'));
            //返回值包括一个base_64加密的图片和一个key
            return $this->success($result);
        }
    
    Example:校验验证码
    <?php
    /**
    *@params key,captcha
    *两个参数key 和验证码
    */
    public function check(){
    $captcha = $request->input('captcha');
            $captcha = strtolower($captcha);
            $key = Cache::get(sprintf(config('constants.cache.captcha_code'), $request->input('key')));
            if(app('captcha')->check($captcha,$key) === false){
                return $this->error('验证码错误');
            }
    }

    Finish,Enjoy it.

  • 相关阅读:
    Scala Ant Tasks
    Git挂钩
    读写文件
    DC10用CSS定位控制网页布局
    table设置colspan属性,列宽显示错位解决方法
    ATM和购物商城-错题集
    python 函数参数多种传递方法
    python 函数 初学
    python 集合 gather
    元组 字体高亮 购物车练习
  • 原文地址:https://www.cnblogs.com/brady-wang/p/12034095.html
Copyright © 2011-2022 走看看