Laravel reCaptcha集成 [英] Laravel reCaptcha integration

查看:93
本文介绍了Laravel reCaptcha集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Laravel软件包在我的Laravel项目中实现reCaptcha.我曾尝试使用经典的reCaptcha V2,但我想改为实现不可见的reCaptcha.

I would like to implement reCaptcha in my Laravel project without using Laravel packages. I have tried with classical reCaptcha V2 which works, but I would like to implement invisible reCaptcha instead.

所以我要做的是

<form id="subscribeForm" class="form-inline" role="form" method="post" action="/subscribe" style="margin-bottom:70px;">
    ...
    ...
    <button type="submit" class="btn btn-default">{{trans('content.input_submit')}}</button>

    <div id="recaptcha" class="g-recaptcha" data-sitekey="---my---key---" data-size="invisible" data-callback="onSubmit"></div>

    <script> ...callback functions... </script>
</form>

我正确显示了浮动的reCaptcha栏,但是当然,因为我需要一个按钮来执行实际的提交,所以我有一个类型为commit的按钮,并且reCaptcha div的回调函数都不会触发.当我返回请求时,我得到g-recaptcha-response为空.

I got the floating reCaptcha bar on the right appear correctly, but of course since I need a button to perform actual submission I have a button with type submit, and none of the callback functions from reCaptcha div get triggered. When I return the request I get g-recaptcha-response empty.

为什么它不独立于回调而提交?

Why doesn't it get submitted independent of the callback?

推荐答案

reCaptcha的回调函数不会被触发,这取决于您定义回调函数的位置.

The callback function of reCaptcha doesn't get triggered, that depends where you have defined your callback function.

不应在$(document).ready()window.onload范围内定义

要将验证码令牌提交到服务器,请在表单中放置一个隐藏的输入字段

To submit the captcha token to the server, place an hidden input field within your form

<input type="hidden" name="reCaptchaToken" value="" id="reCaptchaToken">

将提交按钮替换为常规按钮,以便不提交表单,并在不需要验证码的情况下删除验证码<div>.

Replace the submit button with a regular button so that the form isn't submitted, remove the captcha <div> as it isn't needed.

<button type="button" class="btn btn-default g-recaptcha" data-sitekey="your_site_key" data-callback='onSubmit'>{{trans('content.input_submit')}}</button>

,您可以使用回调函数填充值并提交表单

and you could populate the value using the callback function and submit the form

// Google reCaptcha callback
function onSubmit (res) {
    document.getElementById("reCaptchaToken").value = res;
    document.getElementById("subscribeForm").submit();
}

并使用Input::get('reCaptchaToken')

这篇关于Laravel reCaptcha集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆