github地址:https://github.com/mewebstudio/captcha
1. composer安装
# composer require mews/captcha
2. 在config/app.php的providers中注册Captcha
对于Laravel 5.1+
' providers ' => [
Mews\Captcha\CaptchaServiceProvider::class,
]
3. 再在config/app.php的aliases中添加
对于Laravel 5.1+
' aliases ' => [
'Captcha' => Mews\Captcha\Facades\Captcha::class,
]
4. 配置
要使用自己的设置,发布配置。
$ php artisan vendor:publish
会生成 config/captcha.php
return [
'default' => [
'length' => 5,
'width' => 120,
'height' => 36,
'quality' => 90,
],
// ...
];
5. 示例用法
// [your site path]/Http/routes.php
首先引入类:use Validator;
Route::any('captcha-test', function()
{
$rules = ['captcha' => 'required|captcha'];
$messages = [
'captcha.required' => '验证码为必填项',
'captcha.captcha' => '验证码错误',
];
$validator = Validator::make(Input::all(), $rules,$messages);
if ($validator->fails()){
return back()->withErrors($validator);
}
}
6. 点击验证码切换 js事件
(*要在index.php开头添加session_start(); 不然无法切换)
@if ($errors->has('captcha'))
<div class="alert alert-danger">
{{$errors->first('captcha')}}
</div>
@endif
<input class="input-text size-L" name="captcha" type="text" placeholder="验证码" style="width:150px;">
<img src="{{captcha_src()}}" onclick="this.src='{{captcha_src()}}?+Math.random()'" style='cursor:pointer;' title="看不清?点击换一张">
7. 获取验证码图片的一些方法
①返回图片
captcha(); 或者 Captcha::create();
②返回图片URL
captcha_src(); 或者 Captcha::src();
③返回图片html
captcha_img(); 或者 Captcha::img();
本文链接:https://www.23thi.com/403.html
除非特殊声明,本站文章均为原创,转载请务必保留本文链接