当前位置:网站首页>Laravel文档阅读笔记-mews/captcha的使用(验证码功能)
Laravel文档阅读笔记-mews/captcha的使用(验证码功能)
2022-07-05 12:34:00 【IT1995】
这里用的是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
<?php
return [
'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'],
'default' => [
'length' => 5,
'width' => 120,
'height' => 36,
'quality' => 90,
'math' => false,
'expire' => 60,
'encrypt' => false,
],
......
......
];
这里default.length的大小本来是9的,我改成了5。
生成对应的Controller
php artisan make:controller CaptchaValidationController
在CaptchaValidationController.php中生成验证码:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class CaptchaValidationController extends Controller
{
public function reloadCaptcha()
{
return response()->json(['captcha'=> captcha_img()]);
}
}
这里这个Controller就是验证码服务了。就完成了。
下面是使用。
前端:
JS相关:
<script type="text/javascript">
$('#captchaImg').click(function(){
$(this).prop('src',"{
{captcha_src()}}" + Math.random(1000,9999));
});
</script>
HTML相关:
<div class="row">
<div class="col" style="max-width: 140px">
<img src="{
{ captcha_src() }}" id="captchaImg" >
</div>
<div class="col pull-left ">
<input type="text" autocomplete="off" placeholder="验证码" id="captcha" class="form-control" name="captcha">
</div>
</div>
后端:
public function customLogin(Request $request)
{
$request->validate([
'email' => 'required|email',
'password' => 'required|min:6|max:128',
'captcha' => 'required|captcha'
]);
......
......
}
这里Laravel自己做了处理,我们只要添加了captcha,就可以了。这里感觉比其他语言少写了好多逻辑判断代码。
边栏推荐
- Flume common commands and basic operations
- 激动人心!2022开放原子全球开源峰会报名火热开启!
- Understand redis persistence mechanism in one article
- NLP engineer learning summary and index
- Redis highly available sentinel mechanism
- NPM install reports an error
- View and modify the MySQL data storage directory under centos7
- Distributed solution - Comprehensive decryption of distributed task scheduling platform -xxljob
- 10 minute fitness method reading notes (2/5)
- JDBC -- extract JDBC tool classes
猜你喜欢
SAP self-development records user login logs and other information
上午面了个腾讯拿 38K 出来的,让我见识到了基础的天花
Third party payment interface design
前几年外包干了四年,秋招感觉人生就这样了..
UNIX socket advanced learning diary -ipv4-ipv6 interoperability
Migrate data from Mysql to neo4j database
激动人心!2022开放原子全球开源峰会报名火热开启!
JDBC exercise - query data encapsulated into object return & simple login demo
Transactions from January 6 to October 2022
About LDA model
随机推荐
Pytoch uses torchnet Classerrormeter in meter
GPS數據格式轉換[通俗易懂]
GPON other manufacturers' configuration process analysis
Taobao order interface | order flag remarks, may be the most stable and easy-to-use interface
Knowledge representation (KR)
2021-12-22 transaction record
Learn JVM garbage collection 02 - a brief introduction to the reference and recycling method area
JSON parsing error special character processing (really speechless... Troubleshooting for a long time)
Kotlin function
Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
Master-slave mode of redis cluster
HiEngine:可媲美本地的云原生内存数据库引擎
Taobao flag insertion remarks | logistics delivery interface
Constructing expression binary tree with prefix expression
Handwriting blocking queue: condition + lock
SAP self-development records user login logs and other information
Pytoch through datasets Imagefolder loads datasets directly from files
Deep discussion on the decoding of sent protocol
Difference between JUnit theories and parameterized tests
NLP engineer learning summary and index