Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Recaptcha v3 example demo

Until now, I was working with Google Recaptcha v2, but now I want to update my WebApp using the lastest version (v3).

Is it possible to anyone add a fully working Google Recaptcha v3 example for a basic form as I can't find any working demos of it?

I'd really appreciate it.

Thank you very much.

PS: I'm using Java Servlets on the server side, but it doesn't matter if you explain using PHP or whatever.

like image 519
Ommadawn Avatar asked Jul 24 '18 21:07

Ommadawn


People also ask

How do I test a Google reCAPTCHA v3?

You can test invisible recaptcha by using Chrome emulator. You will need to add a new custom device (BOT) in developer tools, and set User Agent String to Googlebot/2.1 on Desktop . Then use the new BOT device when testing on your site to trigger the recaptcha authentication.

How do I use reCAPTCHA v3 in HTML?

The easiest method for using reCAPTCHA v3 on your page is to include the necessary JavaScript resource and add a few attributes to your html button. Load the JavaScript API. Add a callback function to handle the token. Add attributes to your html button.

Is Google reCAPTCHA v3 free?

reCAPTCHA is a free service that protects your website from spam and abuse. reCAPTCHA uses an advanced risk analysis engine and adaptive challenges to keep automated software from engaging in abusive activities on your site.


1 Answers

Simple code to implement ReCaptcha v3

The basic JS code

<script src="https://www.google.com/recaptcha/api.js?render=your reCAPTCHA site key here"></script> <script>     grecaptcha.ready(function() {     // do request for recaptcha token     // response is promise with passed token         grecaptcha.execute('your reCAPTCHA site key here', {action:'validate_captcha'})                   .then(function(token) {             // add token value to form             document.getElementById('g-recaptcha-response').value = token;         });     }); </script> 

The basic HTML code

<form id="form_id" method="post" action="your_action.php">     <input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response">     <input type="hidden" name="action" value="validate_captcha">     .... your fields </form> 

The basic PHP code

if (isset($_POST['g-recaptcha-response'])) {     $captcha = $_POST['g-recaptcha-response']; } else {     $captcha = false; }  if (!$captcha) {     //Do something with error } else {     $secret   = 'Your secret key here';     $response = file_get_contents(         "https://www.google.com/recaptcha/api/siteverify?secret=" . $secret . "&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']     );     // use json_decode to extract json response     $response = json_decode($response);      if ($response->success === false) {         //Do something with error     } }  //... The Captcha is valid you can continue with the rest of your code //... Add code to filter access using $response . score if ($response->success==true && $response->score <= 0.5) {     //Do something to denied access } 

You have to filter access using the value of $response.score. It can takes values from 0.0 to 1.0, where 1.0 means the best user interaction with your site and 0.0 the worst interaction (like a bot). You can see some examples of use in ReCaptcha documentation.

like image 55
kikerrobles Avatar answered Sep 28 '22 21:09

kikerrobles