google recaptcha v3 implementatie

xxxx= sitekey: https://developers.google.com/recaptcha

naam action maakt niet uit

CLIENT

<script src=’https://www.google.com/recaptcha/api.js?render=xxxx’></script>
<script>
grecaptcha.ready(function() {
grecaptcha.execute(‘xxxx’, {action: ‘contactformx’}).then(function(token) {
$(‘<input>’).attr({
type: ‘hidden’,
name: ‘g-recaptcha-response’,
value: token
}).prependTo(‘.cform’)
});
});

</script>

 

In form <form name=”cform” id=”cform” class=”cform

 

 

SERVER na post form

$params = [];
$params[‘secret’] = ‘xxxx’;
if (!empty($_POST) && isset($_POST[‘g-recaptcha-response’])) {
$params[‘response’] = urlencode($_POST[‘g-recaptcha-response’]);
}
$params[‘remoteip’] = $_SERVER[‘REMOTE_ADDR’];
$params_string = http_build_query($params);
$requestURL = ‘https://www.google.com/recaptcha/api/siteverify?’ . $params_string;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $requestURL,
));
$response = curl_exec($curl);
curl_close($curl);
$response = @json_decode($response, true);
if ($response[“success”] == true) {
// captcha valid
}