Tried many ways to implement google recaptcha, but in my admin console shown message: “your site does not validate reCAPTCHA tokens”.
Script included on the top of HTML page
JavaScript
x
2
1
<script src="https://www.google.com/recaptcha/api.js?render=my_site_key_here"></script>
2
My php realized form
JavaScript
1
13
13
1
<?php
2
function submit_details_form() {
3
$form = <<<HTML
4
<form class="modal-form" id="modal-container-screw" method="post" enctype="multipart/form-data" action="/wp-content/plugins/screw-cat_plugin/send-form.php">
5
<input type="hidden" name="g-recaptcha_response" id="g-recaptcha-response">
6
<input type="hidden" name="action" value="validate_captcha">
7
*other inputs*
8
<button type="submit" name="submitBtn" id='submit-btn-screw'>Submit</button>
9
</form>
10
HTML;
11
return $form; }
12
?>
13
My javascript reCAPTCHA code
JavaScript
1
12
12
1
window.addEventListener("load", function(){
2
grecaptcha.ready(function() {
3
//do request for recaptcha token
4
//response is promise with passed token
5
grecaptcha.execute('6LffySQbAAAAADoL3UKMcHfGAbdiqcaSkq5crucT', {action:'validate_captcha'})
6
.then(function(token) {
7
//add token value to form
8
document.getElementById('g-recaptcha-response').value = token;
9
});
10
});
11
});
12
My php code for submiting for data
JavaScript
1
23
23
1
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
2
$recaptcha_secret = 'my secret reCAPTCHA key here';
3
$recaptcha_response = $_POST['g-recaptcha-response'];
4
// Sending POST request and decode answer results
5
$recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response= . '$recaptcha_response . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
6
print_r($recaptcha);
7
$recaptcha = json_decode($recaptcha);
8
// Тake measures depending on the result
9
if ($recaptcha->score >= 0.3) {
10
$success_msg = "Success!";
11
echo '<script language="javascript">';
12
echo 'alert("Successful spam check.")';
13
echo '</script>';
14
sendWithAttachments("myemail@mail.com", "Some mail theme");
15
} else {
16
$recaptchaToArr = json_decode(json_encode($recaptcha), true);
17
echo '<script language="javascript">';
18
echo 'alert("Failed spam check.")';
19
echo '</script>';
20
}
21
22
print_r($recaptcha); displays { "success": false, "error-codes": [ "invalid-input-response" ] }
23
Advertisement
Answer
The issue I believe is a simple one. The original syntax error as per the comment was a curve ball but on closer inspection and some rudimentary testing reveals that the spelling of g-recaptcha-response
in the PHP
and g-recaptcha_response
in Form
were causing the problem.
JavaScript
1
76
76
1
<?php
2
3
4
define('_PUBKEY','6LffySQbAAAAADoL3UKMcHfGAbdiqcaSkq5crucT');
5
define('_PRIVKEY','xxxxxxxxxxx');
6
define('SEND_MESSAGE',false);
7
8
if( $_SERVER['REQUEST_METHOD'] == 'POST' && isset(
9
$_POST['g-recaptcha-response'],
10
$_POST['action']
11
)){
12
13
$baseurl = 'https://www.google.com/recaptcha/api/siteverify';
14
$secret = _PRIVKEY;
15
$url=sprintf(
16
'%s?secret=%s&response=%s&remoteip=%s',
17
$baseurl,
18
$secret,
19
$_POST['g-recaptcha-response'],
20
$_SERVER['REMOTE_ADDR']
21
);
22
23
$json = json_decode( file_get_contents( $url ) );
24
25
if ( $json->score >= 0.3 ) {
26
echo '
27
<script>
28
alert("Successful spam check.")
29
</script>';
30
31
if( SEND_MESSAGE && function_exists('sendWithAttachments') )sendWithAttachments("myemail@mail.com", "Some mail theme");
32
33
} else {
34
echo '
35
<script>
36
alert("Failed spam check.")
37
</script>';
38
}
39
}
40
41
?>
42
<!DOCTYPE html>
43
<html lang='en'>
44
<head>
45
<meta charset='utf-8' />
46
<title>Google recaptcha</title>
47
<script>
48
<?php
49
printf('
50
const _PUBKEY="%s";
51
',_PUBKEY);
52
?>
53
window.addEventListener("load", function(){
54
grecaptcha.ready(()=>{
55
console.log('ready')
56
grecaptcha.execute( _PUBKEY, { action:'validate_captcha' })
57
.then(( token )=>{
58
document.getElementById('g-recaptcha-response').value = token;
59
});
60
});
61
});
62
</script>
63
<script src='//www.google.com/recaptcha/api.js?render=<?php echo _PUBKEY;?>' async defer></script>
64
</head>
65
<body>
66
67
<form class='modal-form' id='modal-container-screw' method='post' enctype='multipart/form-data'>
68
<input type='text' name='g-recaptcha-response' id='g-recaptcha-response' />
69
<input type='hidden' name='action' value='validate_captcha' />
70
<input type='submit' />
71
</form>
72
73
74
</body>
75
</html>
76