Skip to content

Instantly share code, notes, and snippets.

@benzkji
Last active March 25, 2021 16:41
Show Gist options
  • Save benzkji/4078af592c97810fdb141b5937a9aaf9 to your computer and use it in GitHub Desktop.
Save benzkji/4078af592c97810fdb141b5937a9aaf9 to your computer and use it in GitHub Desktop.
simple invisible recaptcha example, works with multiple forms
<script src='https://www.google.com/recaptcha/api.js?hl=de'></script>
check https://developers.google.com/recaptcha/docs/invisible
<div
id="header_recaptcha"
class="g-recaptcha"
data-sitekey="6LctdiQUAAAAAOTUOX92-PkGJXpZgGUp5hrq4l65"
data-size="invisible"
data-callback="recaptcha_submit"
></div>
<input type="hidden" name="recaptcha" value="">
var on_code_form_submit = function(e) {
var response = grecaptcha.getResponse();
if (!response) {
e.preventDefault();
$(this).attr('data-submit-please', 'true');
grecaptcha.execute();
} else {
$(this).find('input[name="recaptcha"]').val(response);
}
};
function recaptcha_submit(token) {
var $form = $('form[data-submit-please="true"]');
$form.find('input[name="recaptcha"]').val(token)
$form.submit();
}
def is_valid_recaptcha(request, token_response):
ip = get_client_ip(request)
secret = settings.RECAPTCHA_SECRET
data = {
'secret': secret,
'response': token_response,
'remoteip': ip,
}
url = 'https://www.google.com/recaptcha/api/siteverify'
http_response = requests.post(url, data)
json = http_response.json()
if json['success'] == True:
return True
return False
@lukemagdalin
Copy link

you might not want to show your key

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment