lshorz/luocaptcha

鲁诺验证码的Laravel扩展

1.0 2018-03-29 06:50 UTC

This package is auto-updated.

Last update: 2024-09-21 20:15:42 UTC


README

基于Luosimao免费人机验证的Laravel 5.x扩展

介绍

安装

$ composer require "lshorz/luocaptcha":"dev-master"

如果Laravel版本小于5.5,则需要修改config/app.php,并增加以下内容:

Lshorz\Luosimao\LCaptchaServiceProvider::class

增加别名

'LCaptcha' => Lshorz\Luosimao\Facades\LCaptcha::class,

配置

1.执行

$ php artisan vendor:publish --tag=lcaptcha

2.前往官方网站注册账号并配置config/lcaptcha.php

使用方法

在页面头部等合适的位置添加以下代码:

<script src="//captcha.luosimao.com/static/js/api.js"></script>

在需要显示验证组件的地方插入以下代码:

/**
* @param string $width  组件宽度
* @param string $callback 客户端验证成功回调函数名称
*/
{!! LCaptcha::render('100%', 'callback') !!}

这将生成类似以下HTML代码:

<div class="l-captcha" data-width="200" data-site-key="xxxxxxxxxxxxxx" data-callback="callback"></div>

此外,您还需要自行补充JS方法,例如将获取的response(默认参数名为:luotest_response)参数发送到服务器进行远程验证,参考如下:

<script>
    function callback(resp){
        $.post('/check', {"luotest_response": resp}, function(res){
            console.log(res);
        });
    }
</script>

服务端验证

方法一:

$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';
}

方法二:

use Lshorz\Luocaptcha\Facades\LCaptcha;

...

$res = LCaptcha::verify($request->only('luotest_response'));
if ($res) {
    echo 'passed';
} else {
    echo 'fail';
}

其他请参考:

鲁诺官方使用文档