f9webltd / invisible-recaptcha
为Laravel的无痕reCAPTCHA。
Requires
- php: ^8.0
- guzzlehttp/guzzle: ^7.0
- illuminate/support: ^10.0|^11.0
- illuminate/view: ^10.0|^11.0
Requires (Dev)
- phpunit/phpunit: ^9.3
This package is auto-updated.
Last update: 2024-08-29 08:14:03 UTC
README
背景
2024年6月28日 - 原始仓库已超过两年未更新,不支持Laravel 11,且缺少关于修复最近polyfillio[点]io攻击的关键PR。此分支包括修复和放弃对旧Laravel和PHP版本的支持。我计划整理分支仓库,添加GitHub工作流程并设置不同的命名空间。
支持
Laravel 10 / 11,PHP *8.0
。
安装
composer require f9webltd/invisible-recaptcha
设置
将ServiceProvider添加到app/config/app.php
中的providers数组中。
AlbertCht\InvisibleReCaptcha\InvisibleReCaptchaServiceProvider::class,
配置
在设置配置之前,请记住在申请密钥时选择无痕reCAPTCHA
。
将INVISIBLE_RECAPTCHA_SITEKEY
和INVISIBLE_RECAPTCHA_SECRETKEY
添加到.env
文件中。
// required
INVISIBLE_RECAPTCHA_SITEKEY={siteKey}
INVISIBLE_RECAPTCHA_SECRETKEY={secretKey}
// optional
INVISIBLE_RECAPTCHA_BADGEHIDE=false
INVISIBLE_RECAPTCHA_DATABADGE='bottomright'
INVISIBLE_RECAPTCHA_TIMEOUT=5
INVISIBLE_RECAPTCHA_DEBUG=false
您可以选择三种不同的验证码样式:
bottomright
、bottomleft
、inline
如果您将
INVISIBLE_RECAPTCHA_BADGEHIDE
设置为true,则可以隐藏徽标logo。
您可以通过将
INVISIBLE_RECAPTCHA_DEBUG
设置为true来在浏览器控制台中查看这些验证码元素的绑定状态。
使用方法
在渲染验证码之前,请注意以下事项
render()
或renderHTML()
函数必须在表单元素内调用。- 您必须确保您的提交按钮的
type
属性为submit
。 - 您的表单中只能有一个提交按钮。
在您的视图中显示reCAPTCHA
{!! app('captcha')->render() !!} // or you can use this in blade @captcha
支持自定义语言
{!! app('captcha')->render('en') !!} // or you can use this in blade @captcha('en')
与VueJS等JavaScript框架一起使用
渲染过程包括三个不同的部分,可以在使用VueJS等框架时分别渲染,因为这些框架在模板中包含<script>
标签时会抛出控制台错误。
您可以在HTML的头部位置渲染polyfill。
{!! app('captcha')->renderPolyfill() !!} // Or with blade directive: @captchaPolyfill
您可以使用以下方式渲染HTML,这需要在您的<form>
标签内部进行
{!! app('captcha')->renderCaptchaHTML() !!} // Or with blade directive: @captchaHTML
您可以使用以下方式渲染必要的<script>
标签,包括可选的语言支持
// The argument is optional. {!! app('captcha')->renderFooterJS('en') !!} // Or with blade directive: @captchaScripts // blade directive, with language support: @captchaScripts('en')
验证
将'g-recaptcha-response' => 'required|captcha'
添加到规则数组中。
$validate = Validator::make(Input::all(), [ 'g-recaptcha-response' => 'required|captcha' ]);
CodeIgniter 3.x
在application/config/config.php中设置
$config['composer_autoload'] = TRUE;
在application/config/config.php中添加行
$config['recaptcha.sitekey'] = 'sitekey'; $config['recaptcha.secret'] = 'secretkey'; // optional $config['recaptcha.options'] = [ 'hideBadge' => false, 'dataBadge' => 'bottomright', 'timeout' => 5, 'debug' => false ];
在控制器中使用
$data['captcha'] = new \AlbertCht\InvisibleReCaptcha\InvisibleReCaptcha( $this->config->item('recaptcha.sitekey'), $this->config->item('recaptcha.secret'), $this->config->item('recaptcha.options'), );
在视图中,在您的表单中
<?php echo $captcha->render(); ?>
然后回到您的控制器中验证它
$captcha->verifyResponse($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
没有Laravel或CodeIgniter
查看以下示例
<?php require_once "vendor/autoload.php"; $siteKey = 'sitekey'; $secretKey = 'secretkey'; // optional $options = [ 'hideBadge' => false, 'dataBadge' => 'bottomright', 'timeout' => 5, 'debug' => false ]; $captcha = new \AlbertCht\InvisibleReCaptcha\InvisibleReCaptcha($siteKey, $secretKey, $options); // you can override single option config like this $captcha->setOption('debug', true); if (!empty($_POST)) { var_dump($captcha->verifyResponse($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR'])); exit(); } ?> <form action="?" method="POST"> <?php echo $captcha->render(); ?> <button type="submit">Submit</button> </form>
控制提交功能
仅在您需要在点击提交按钮后完全控制时使用此函数。如果您在此函数中返回false,则不会触发reCAPTCHA验证。
_beforeSubmit = function(e) { console.log('submit button clicked.'); // do other things before captcha validation // e represents reference to original form submit event // return true if you want to continue triggering captcha validation, otherwise return false return false; }
自定义提交功能
如果您想自定义提交函数,例如:在点击提交按钮后执行某些操作或将提交改为ajax调用等。
您需要做的就是实现javascript中的_submitEvent
_submitEvent = function() { console.log('submit button clicked.'); // write your logic here // submit your form _submitForm(); }
以下是一个使用ajax提交(使用jQuery选择器)的示例
_submitEvent = function() { $.ajax({ type: "POST", url: "{{route('message.send')}}", data: { "name": $("#name").val(), "email": $("#email").val(), "content": $("#content").val(), // important! don't forget to send `g-recaptcha-response` "g-recaptcha-response": $("#g-recaptcha-response").val() }, dataType: "json", success: function(data) { // success logic }, error: function(data) { // error logic } }); };
示例仓库
仓库:https://github.com/albertcht/invisible-recaptcha-example
这个仓库展示了如何以Ajax方式使用这个包。
展示
致谢
- anhskohbo(no-captcha包的作者)
- 贡献者
在Beerpay上支持
嘿,兄弟!帮我来点🍻吧!