当前位置:网站首页>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,就可以了。这里感觉比其他语言少写了好多逻辑判断代码。
边栏推荐
- ZABBIX 5.0 - LNMP environment compilation and installation
- JDBC -- extract JDBC tool classes
- Resnet+attention project complete code learning
- Learn the memory management of JVM 02 - memory allocation of JVM
- [superhard core] is the core technology of redis
- Summary of C language learning problems (VS)
- Distance measuring sensor chip 4530a used in home intelligent lighting
- SAP self-development records user login logs and other information
- Implementing Yang Hui triangle with cyclic queue C language
- 超高效!Swagger-Yapi的秘密
猜你喜欢
Summary of C language learning problems (VS)
JSON parsing error special character processing (really speechless... Troubleshooting for a long time)
Pytoch loads the initialization V3 pre training model and reports an error
A few years ago, I outsourced for four years. Qiu Zhao felt that life was like this
Redis highly available slice cluster
Pytoch uses torchnet Classerrormeter in meter
MySQL transaction
Distance measuring sensor chip 4530a used in home intelligent lighting
How to connect the API interface of Taobao open platform (super detailed)
Distributed cache architecture - cache avalanche & penetration & hit rate
随机推荐
NFT: how to make money with unique assets?
The relationship between the size change of characteristic graph and various parameters before and after DL convolution operation
Introduction to GNN
[hdu 2096] Xiaoming a+b
Volatile instruction rearrangement and why instruction rearrangement is prohibited
Just a coincidence? The mysterious technology of apple ios16 is actually the same as that of Chinese enterprises five years ago!
Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
Rasa Chat Robot Tutorial (translation) (1)
Distributed solution - completely solve website cross domain requests
Redis highly available sentinel cluster
How to recover the information server and how to recover the server data [easy to understand]
GPS數據格式轉換[通俗易懂]
Full text search of MySQL
I met Tencent in the morning and took out 38K, which showed me the basic smallpox
Difference between JUnit theories and parameterized tests
Solve the problem of cache and database double write data consistency
Understand redis persistence mechanism in one article
Iterator details in list... Interview pits
Pytoch through datasets Imagefolder loads datasets directly from files
JDBC exercise - query data encapsulated into object return & simple login demo