• Laravel文档阅读笔记-mews/captcha的使用(验证码功能)


    这里用的是Laravel8。

    这里验证码要使用到fileInfo的功能,需要提前让php加载此dll or so,在php.ini中修改:

    验证码功能最终实现的效果是:

     

    首先添加相关库:

    composer require mews/captcha --ignore-platform-req=ext-fileinfo

     其次再生成对应的config文件:

    php artisan vendor:publish

    输入你那边mews/captcha相关的选项:

    我这里是选项11。

    默认验证码为9位,这里太多了,看不清,修改下captcha.php

    1. <?php
    2. return [
    3. 'characters' => ['2', '3', '4', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'm', 'n', 'p', 'q', 'r', 't', 'u', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'M', 'N', 'P', 'Q', 'R', 'T', 'U', 'X', 'Y', 'Z'],
    4. 'default' => [
    5. 'length' => 5,
    6. 'width' => 120,
    7. 'height' => 36,
    8. 'quality' => 90,
    9. 'math' => false,
    10. 'expire' => 60,
    11. 'encrypt' => false,
    12. ],
    13. ......
    14. ......
    15. ];

    这里default.length的大小本来是9的,我改成了5。

    生成对应的Controller

    php artisan make:controller CaptchaValidationController

    在CaptchaValidationController.php中生成验证码:

    1. <?php
    2. namespace App\Http\Controllers;
    3. use Illuminate\Http\Request;
    4. class CaptchaValidationController extends Controller
    5. {
    6. public function reloadCaptcha()
    7. {
    8. return response()->json(['captcha'=> captcha_img()]);
    9. }
    10. }

    这里这个Controller就是验证码服务了。就完成了。

    下面是使用。

    前端:

    JS相关:

    1. <script type="text/javascript">
    2. $('#captchaImg').click(function(){
    3. $(this).prop('src',"{{captcha_src()}}" + Math.random(1000,9999));
    4. });
    5. </script>

     HTML相关:

    1. <div class="row">
    2. <div class="col" style="max-width: 140px">
    3. <img src="{{ captcha_src() }}" id="captchaImg" >
    4. </div>
    5. <div class="col pull-left ">
    6. <input type="text" autocomplete="off" placeholder="验证码" id="captcha" class="form-control" name="captcha">
    7. </div>
    8. </div>

    后端:

    1. public function customLogin(Request $request)
    2. {
    3. $request->validate([
    4. 'email' => 'required|email',
    5. 'password' => 'required|min:6|max:128',
    6. 'captcha' => 'required|captcha'
    7. ]);
    8. ......
    9. ......
    10. }

    这里Laravel自己做了处理,我们只要添加了captcha,就可以了。这里感觉比其他语言少写了好多逻辑判断代码。

  • 相关阅读:
    计算机毕业设计ssm工作室管理系统v186g系统+程序+源码+lw+远程部署
    iOS 那些不为人知的bug: Error Domain=NSCocoaErrorDomain Code=3840
    解密prompt系列5. APE+SELF=自动化指令集构建代码实现
    如何解决Word文件打不开呢?几个简单的步骤
    如何创建与引擎独立的Iceberg表
    Mac下安装Hadoop
    Splunk UBA 从 Ldap 成功导入 HR 数据
    【MySQL】sql语句之库操作
    ant-design中form组件的item在typescript环境下自定义提示文字(Form.Item的tooltip属性)及提示图标
    DusQ1 dT phosphoramidite,DusQ1-dT磷酰胺
  • 原文地址:https://blog.csdn.net/qq78442761/article/details/125612831