Luosimao 人机验证 Laravel 5.X 扩展
基于Luosimao免费人机验证的Laravel 5.x扩展, (*1)
, (*2)
安装
$ composer require "lshorz/luocaptcha":"dev-master"
如果是laravel<5.5 则修改config/app.php
增加, (*3)
Lshorz\Luosimao\LCaptchaServiceProvider::class
增加aliases, (*4)
'LCaptcha' => Lshorz\Luosimao\Facades\LCaptcha::class,
配置
1.执行, (*5)
$ php artisan vendor:publish --tag=lcaptcha
2.去官注册帐号并配置config/lcaptcha.php, (*6)
使用方法
在页面head等合适的地方加入, (*7)
<script src="//captcha.luosimao.com/static/js/api.js"></script>
在需要显示该验证组件的地方插入以下代码, (*8)
/**
* @param string $width 组件宽度
* @param string $callback 客户端验证成功回调函数名称
*/
{!! LCaptcha::render('100%', 'callback') !!}
将会生成类似代html代码, (*9)
<div class="l-captcha" data-width="200" data-site-key="xxxxxxxxxxxxxx" data-callback="callback"></div>
另外需要自己补充JS方法,例如将取得的response(默认参数名为:luotest_response)参数post到服务器进行远程验证,参考如下:, (*10)
<script>
function callback(resp){
$.post('/check', {"luotest_response": resp}, function(res){
console.log(res);
});
}
</script>
服务端验证
方法一:, (*11)
$v = Validator::make($request->only('luotest_response'), ['luotest_response'=>'required|lcaptcha'],
[
'luotest_response.required' => '不得为空',
'luotest_response.lcaptcha'=>'验证失败'
]);
if ($v->fails()) {
return $v->errors();
} else {
return 'ok';
}
方法二:, (*12)
use Lshorz\Luocaptcha\Facades\LCaptcha;
...
$res = LCaptcha::verify($request->only('luotest_response'));
if ($res) {
echo 'passed';
} else {
echo 'fail';
}
其他请参考:, (*13)