adding google recaptcha to html form

find all your recaptcha info and statistics

 

https://www.google.com/recaptcha/admin

 

 

go to:

https://www.google.com/recaptcha/admin/create

when you register your recaptcha, you will get 2 codes.

site key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

secret key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

 

add the code to your:

– header

<script async defer src="https://www.google.com/recaptcha/api.js" ></script>

 

– form

<div style="padding-left: 12%; width: 100%" class="g-recaptcha" data-sitekey="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"></div>

 

 

– Then, make sure that your apache configuracion allows to use iframe, specifically the Content-Security-Policy

 

 

Then add the validating code to your internal “logics” page, I do this in PHP.

 

 

 

$url = 'https://www.google.com/recaptcha/api/siteverify';
$privatekey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

$response = file_get_contents($url."?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
$data = json_decode($response);

//echo "$response: ".$response."\n\n\n\n\n";
//echo "$data: ".$data."\n\n\n\n\n";

echo "</br>captcha: validating";

if(isset($data->success) AND $data->success==true)

DO WHATEVER YOU WANT YOUR CODE TO DO!

 

Leave a Reply

Your email address will not be published. Required fields are marked *