Не удается настроить, почему не работает reCaptcha V3 - PullRequest
1 голос
/ 18 июня 2019

Я пытаюсь добавить Google reCaptcha V3 в форму, я искал ее и нашел несколько статей и фрагментов кода из другого вопроса.

Я получаю false каждый раз, когда отправляюформа.

Вот код HTML:

<script src='https://www.google.com/recaptcha/api.js?render=My Website Key'></script>
</head>

Код JS:

<body>
    <script>
        $('#mailForm').submit(function() { 
            event.preventDefault();
             grecaptcha.ready(function() {
                 grecaptcha.execute('The Website Key', {action: 'homepage'}).then(function(token) {
                    $('#mailForm').prepend('<input type="hidden" name="token" value="' + token + '">');
                    $('#mailForm').prepend('<input type="hidden" name="action" value="homepage">');
                    $('#mailForm').unbind('submit').submit();
                });;
            });
        });
    </script>

Форма:

<form action='php/mail.php' method="post" id='mailForm' enctype='multipart/form-data'>
    //Some inputs 
</form>

PHPкод:

if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {
    $secret = 'My Secret Key';
    //get verify response data
    $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
    $responseData = json_decode($verifyResponse);
    if($responseData->success){
        //Send the mail
    }else{
        echo 'Please check reCaptcha';
    }
}else{
    echo 'Please check reCaptcha';
}

Почему это не работает, и я продолжаю получать Please check reCaptcha?

...