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.

  • 相关阅读:
    求1977!
    三进制小数
    回文数C语言
    JAVA知识点必看
    servlet HttpServletRequest
    为什么web工程要输入localhost或者是127.0.0.1
    service $sce or ng-bind-html
    jQuery的deferred对象详解
    理解promise
    理解Angular中的$apply()以及$digest()
  • 原文地址:https://www.cnblogs.com/php-linux/p/12034095.html
Copyright © 2011-2022 走看看