laravel5.3验证码的使用
1,Composer 安装 Captcha 扩展包
- PS:Windows中使用该扩展包还需要安装 GD2 扩展(在php.ini中取消extension=php_gd2.dll前面的注释)
- PS:Windows中使用该扩展包还需要安装 php_fileinfo扩展(在php.ini中取消extension=php_fileinfo.dll前面的注释)
在cmd命令行切换到当前的项目根目录,执行
composer require mews/captcha 这里需要等待一会,因为是国外镜像
2,使用Captcha服务提供者之前还需要在config/app.php中注册服务提供者
a,在 providers 数组内追加如下内容
'providers' => [
Mews\Captcha\CaptchaServiceProvider::class,
]
b,在 aliases 数组内追加如下内容
'aliases' => [
// ...
'Captcha' => Mews\Captcha\Facades\Captcha::class,
]
3,生成验证码配置文件
确保执行命令的时候在项目的根目录里面。
小编这里分享两个命令,执行任意一个即可。
命令1:php artisan vendor:publish --provider="Mews\Captcha\CaptchaServiceProvider"
命令2:php artisan vendor:publish 执行此命令的时候会弹出让选择安装的功能。不建议使用,因为有时候会报错。
4,执行完命令后,安装成功后,这时候会在config目录下生成一个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' => 9,
'width' => 120,
'height' => 36,
'quality' => 90,
'math' => false,
],
'math' => [
'length' => 9,
'width' => 120,
'height' => 36,
'quality' => 90,
'math' => true,
],
'flat' => [
'length' => 6,
'width' => 160,
'height' => 46,
'quality' => 90,
'lines' => 6,
'bgImage' => false,
'bgColor' => '#ecf2f4',
'fontColors' => ['#2c3e50', '#c0392b', '#16a085', '#c0392b', '#8e44ad', '#303f9f', '#f57c00', '#795548'],
'contrast' => -5,
],
'mini' => [
'length' => 3,
'width' => 60,
'height' => 32,
],
'inverse' => [
'length' => 5,
'width' => 120,
'height' => 36,
'quality' => 90,
'sensitive' => true,
'angle' => 12,
'sharpen' => 10,
'blur' => 2,
'invert' => true,
'contrast' => -5,
]
];
5,使用二维码
captcha_img();//使用默认的验证码配置生成验证码并返回img格式
captcha_img('flat');//使用指定的验证码配置生成验证码并返回img格式
6,在模板中调用
ajax提交方式:
<label style="margin-top:-12px;" class="block clearfix">
<span class="block input-icon input-icon-right">
<input type="text" class="form-control" name="code" placeholder="输入验证码" />
<img id="code" style="margin-top:10px;float: right" src="{{ captcha_src('flat') }}" onclick="this.src='/captcha/flat?'+Math.random()" title="点击图片
重新获取验证码">
</span>
</label>
表单提交方式:
<form name="form" action="/check" method="post">
<img src="{{ captcha_src('flat') }}" onclick="this.src='/captcha/flat?'+Math.random()" title="点击图片重新获取验证码">
<input type="text" name="captcha">
{{csrf_field()}}
@if($errors->has('captcha'))
<span>{{$errors->first('captcha')}}</span>
@endif
<input type="submit" value="验证"/>
</form>
7,也可以在php端直接调用在传到前端
返回验证码的 url 地址
captcha_src();//使用默认的验证码配置生成验证码并返回url格式
captcha_src('flat');//使用指定的验证码配置生成验证码并返回url格式
8,控制器验证
a,使用验证码规则验证
public function check(Request $request)
{
$data = $request->all();
$rules = [
'captcha' => 'required|captcha',//required表示必填 captcha表示进行验证码验证
];
// 自定义消息
$messages = [
'captcha.required' => '请输入验证码',
'captcha.captcha' => '请输入正确验证码',
];
//对验证码字段进行验证
$validator = \Validator::make($data, $rules, $messages);
if($validator->passes()){
//验证通过
}else{
//验证失败
//$validator->errors() 获取错误信息
//返回上个页面并将错误信息返回到页面上
return back()->withErrors($validator);
}
}
b,直接使用拓展包的captcha_check函数验证
public function check(Request $request)
{
$data = $request->all();
//对验证码进行验证,验证通过返回true,失败返回false
$capt = captcha_check($data['captcha']);
if($capt){
//验证成功
}else{
//验证失败
}
}