是什么错误导致我的 reCAPTCHA returns 消息 "not validate reCAPTCHA tokens"?
what a mistake that makes my reCAPTCHA returns message "not validate reCAPTCHA tokens"?
尝试了很多方法来实施 google recaptcha,但在我的管理控制台中显示消息:“您的站点未验证 reCAPTCHA 令牌”。
HTML 页面顶部包含脚本
<script src="https://www.google.com/recaptcha/api.js?render=my_site_key_here"></script>
我的php实现形式
<?php
function submit_details_form() {
$form = <<<HTML
<form class="modal-form" id="modal-container-screw" method="post" enctype="multipart/form-data" action="/wp-content/plugins/screw-cat_plugin/send-form.php">
<input type="hidden" name="g-recaptcha_response" id="g-recaptcha-response">
<input type="hidden" name="action" value="validate_captcha">
*other inputs*...
<button type="submit" name="submitBtn" id='submit-btn-screw'>Submit</button>
</form>
HTML;
return $form; }
?>
我的 javascript reCAPTCHA 代码
window.addEventListener("load", function(){
grecaptcha.ready(function() {
//do request for recaptcha token
//response is promise with passed token
grecaptcha.execute('6LffySQbAAAAADoL3UKMcHfGAbdiqcaSkq5crucT', {action:'validate_captcha'})
.then(function(token) {
//add token value to form
document.getElementById('g-recaptcha-response').value = token;
});
});
});
我的php提交数据的代码
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
$recaptcha_secret = 'my secret reCAPTCHA key here';
$recaptcha_response = $_POST['g-recaptcha-response'];
// Sending POST request and decode answer results
$recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response= . '$recaptcha_response . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
print_r($recaptcha);
$recaptcha = json_decode($recaptcha);
// Тake measures depending on the result
if ($recaptcha->score >= 0.3) {
$success_msg = "Success!";
echo '<script language="javascript">';
echo 'alert("Successful spam check.")';
echo '</script>';
sendWithAttachments("myemail@mail.com", "Some mail theme");
} else {
$recaptchaToArr = json_decode(json_encode($recaptcha), true);
echo '<script language="javascript">';
echo 'alert("Failed spam check.")';
echo '</script>';
}
print_r($recaptcha); displays { "success": false, "error-codes": [ "invalid-input-response" ] }
我认为这个问题很简单。根据评论的原始语法错误是曲线球,但仔细检查和一些基本测试表明 PHP
中的 g-recaptcha-response
和 Form
中的 g-recaptcha_response
的拼写是导致问题。
<?php
define('_PUBKEY','6LffySQbAAAAADoL3UKMcHfGAbdiqcaSkq5crucT');
define('_PRIVKEY','xxxxxxxxxxx');
define('SEND_MESSAGE',false);
if( $_SERVER['REQUEST_METHOD'] == 'POST' && isset(
$_POST['g-recaptcha-response'],
$_POST['action']
)){
$baseurl = 'https://www.google.com/recaptcha/api/siteverify';
$secret = _PRIVKEY;
$url=sprintf(
'%s?secret=%s&response=%s&remoteip=%s',
$baseurl,
$secret,
$_POST['g-recaptcha-response'],
$_SERVER['REMOTE_ADDR']
);
$json = json_decode( file_get_contents( $url ) );
if ( $json->score >= 0.3 ) {
echo '
<script>
alert("Successful spam check.")
</script>';
if( SEND_MESSAGE && function_exists('sendWithAttachments') )sendWithAttachments("myemail@mail.com", "Some mail theme");
} else {
echo '
<script>
alert("Failed spam check.")
</script>';
}
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>Google recaptcha</title>
<script>
<?php
printf('
const _PUBKEY="%s";
',_PUBKEY);
?>
window.addEventListener("load", function(){
grecaptcha.ready(()=>{
console.log('ready')
grecaptcha.execute( _PUBKEY, { action:'validate_captcha' })
.then(( token )=>{
document.getElementById('g-recaptcha-response').value = token;
});
});
});
</script>
<script src='//www.google.com/recaptcha/api.js?render=<?php echo _PUBKEY;?>' async defer></script>
</head>
<body>
<form class='modal-form' id='modal-container-screw' method='post' enctype='multipart/form-data'>
<input type='text' name='g-recaptcha-response' id='g-recaptcha-response' />
<input type='hidden' name='action' value='validate_captcha' />
<input type='submit' />
</form>
</body>
</html>
尝试了很多方法来实施 google recaptcha,但在我的管理控制台中显示消息:“您的站点未验证 reCAPTCHA 令牌”。
HTML 页面顶部包含脚本
<script src="https://www.google.com/recaptcha/api.js?render=my_site_key_here"></script>
我的php实现形式
<?php
function submit_details_form() {
$form = <<<HTML
<form class="modal-form" id="modal-container-screw" method="post" enctype="multipart/form-data" action="/wp-content/plugins/screw-cat_plugin/send-form.php">
<input type="hidden" name="g-recaptcha_response" id="g-recaptcha-response">
<input type="hidden" name="action" value="validate_captcha">
*other inputs*...
<button type="submit" name="submitBtn" id='submit-btn-screw'>Submit</button>
</form>
HTML;
return $form; }
?>
我的 javascript reCAPTCHA 代码
window.addEventListener("load", function(){
grecaptcha.ready(function() {
//do request for recaptcha token
//response is promise with passed token
grecaptcha.execute('6LffySQbAAAAADoL3UKMcHfGAbdiqcaSkq5crucT', {action:'validate_captcha'})
.then(function(token) {
//add token value to form
document.getElementById('g-recaptcha-response').value = token;
});
});
});
我的php提交数据的代码
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
$recaptcha_secret = 'my secret reCAPTCHA key here';
$recaptcha_response = $_POST['g-recaptcha-response'];
// Sending POST request and decode answer results
$recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response= . '$recaptcha_response . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
print_r($recaptcha);
$recaptcha = json_decode($recaptcha);
// Тake measures depending on the result
if ($recaptcha->score >= 0.3) {
$success_msg = "Success!";
echo '<script language="javascript">';
echo 'alert("Successful spam check.")';
echo '</script>';
sendWithAttachments("myemail@mail.com", "Some mail theme");
} else {
$recaptchaToArr = json_decode(json_encode($recaptcha), true);
echo '<script language="javascript">';
echo 'alert("Failed spam check.")';
echo '</script>';
}
print_r($recaptcha); displays { "success": false, "error-codes": [ "invalid-input-response" ] }
我认为这个问题很简单。根据评论的原始语法错误是曲线球,但仔细检查和一些基本测试表明 PHP
中的 g-recaptcha-response
和 Form
中的 g-recaptcha_response
的拼写是导致问题。
<?php
define('_PUBKEY','6LffySQbAAAAADoL3UKMcHfGAbdiqcaSkq5crucT');
define('_PRIVKEY','xxxxxxxxxxx');
define('SEND_MESSAGE',false);
if( $_SERVER['REQUEST_METHOD'] == 'POST' && isset(
$_POST['g-recaptcha-response'],
$_POST['action']
)){
$baseurl = 'https://www.google.com/recaptcha/api/siteverify';
$secret = _PRIVKEY;
$url=sprintf(
'%s?secret=%s&response=%s&remoteip=%s',
$baseurl,
$secret,
$_POST['g-recaptcha-response'],
$_SERVER['REMOTE_ADDR']
);
$json = json_decode( file_get_contents( $url ) );
if ( $json->score >= 0.3 ) {
echo '
<script>
alert("Successful spam check.")
</script>';
if( SEND_MESSAGE && function_exists('sendWithAttachments') )sendWithAttachments("myemail@mail.com", "Some mail theme");
} else {
echo '
<script>
alert("Failed spam check.")
</script>';
}
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>Google recaptcha</title>
<script>
<?php
printf('
const _PUBKEY="%s";
',_PUBKEY);
?>
window.addEventListener("load", function(){
grecaptcha.ready(()=>{
console.log('ready')
grecaptcha.execute( _PUBKEY, { action:'validate_captcha' })
.then(( token )=>{
document.getElementById('g-recaptcha-response').value = token;
});
});
});
</script>
<script src='//www.google.com/recaptcha/api.js?render=<?php echo _PUBKEY;?>' async defer></script>
</head>
<body>
<form class='modal-form' id='modal-container-screw' method='post' enctype='multipart/form-data'>
<input type='text' name='g-recaptcha-response' id='g-recaptcha-response' />
<input type='hidden' name='action' value='validate_captcha' />
<input type='submit' />
</form>
</body>
</html>