hnllyrp / laravel-captcha
laravel 的图形验证码包
v1.0.0
2023-10-22 12:04 UTC
Requires
- php: ^7.2.5|^8.0
- laravel/framework: ^6.0
README
laravel 的图形验证码包 图形验证码
-
支持多种类型的验证码:数字字母验证码,算术验证码,中文验证码
-
支持在多个页面使用(需要配置不同的类型)
-
支持 API 接口使用
安装
composer require hnllyrp/laravel-captcha
配置
config/captcha.php
- 支持 type 默认为 default,算术为 mathe,背景图为 image,中文为 chinese。还可以自由组合以支持更多配置
- 其他配置:cache 和 session 可以使用 redis 驱动
使用
- Web 端 demo
<!-- captcha.blade.php -->
<input type="text" id="captcha" name="captcha" />
<img src="{{captcha_src()}}" onclick="this.src='{{captcha_src()}}'+Math.random()"/>
// 验证
$type = $request->input('type', 'default');
$validator = Validator::make($request->all(), [
'captcha' => ['required', 'captcha:' . $type],
]);
// 或使用输助函数
if(!captcha_check($type, $value)){
// 验证失败
}
- API 端 demo
$.get("api/captcha",{}, function(data){
var captcha = data.captcha; // base64图片
var hash = data.hash; // 验证hash,验证接口须带上此参数
$("#captcha_img").src(captcha);
});
// 前端需要把验证码的值与 hash值 去请求 后续的验证接口
// 后端接口验证
$type = $request->input('type', 'default');
$hash = $request->input('hash');
$validator = Validator::make($request->all(), [
'captcha' => ['required', 'captcha_api:' . $type . ',' . $hash],
]);
// 或使用输助函数
if(!captcha_api_check($type, $value, $hash)){
// 验证失败
}