Loading...

Устанавливаем Google reCAPTCHA v3 на сайт

Рабочая инструкция:

1) Регистрируемся на google или авторизуемся под существующим аккаунтом.

Далее переходим по данной ссылке https://www.google.com/recaptcha/intro/v3.html и нажимаем на кнопку “Admin console”

2) Вам нужно скопировать данные ключи.

 

 3) Код для Google reCaptcha

Весь код:

    <?php
    /*КЛЮЧИ*/
    define('SITE_KEY', '_Your_sitekey_');
    define('SECRET_KEY', '_Your_secretkey_');
     
    /*ОБРАБОТКА ЗАПРОСА*/
    if($_POST){
    /*СОЗДАЕМ ФУНКЦИЮ КОТОРАЯ ДЕЛАЕТ ЗАПРОС НА GOOGLE СЕРВИС*/
    function getCaptcha($SecretKey) {
    $Response = file_get_contents("{$SecretKey}");
    $Return = json_decode($Response);
    return $Return;
    }
    /*ПРОИЗВОДИМ ЗАПРОС НА GOOGLE СЕРВИС И ЗАПИСЫВАЕМ ОТВЕТ*/
    $Return = getCaptcha($_POST['g-recaptcha-response']);
    /*ВЫВОДИМ НА ЭКРАН ПОЛУЧЕННЫЙ ОТВЕТ*/
    var_dump($Return);
    /*ЕСЛИ ЗАПРОС УДАЧНО ОТПРАВЛЕН И ЗНАЧЕНИЕ score БОЛЬШЕ 0,5 ВЫПОЛНЯЕМ КОД*/
    if($Return->success == true && $Return->score > 0.5){
    echo "Succes!";
    }
    else {
    echo "You are Robot";
    }
    }
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Document</title>
    </head>
    <body>
    <form action="/" method="POST">
    <input type="text" name="name" /><br/><br/>
    <input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response" /><br/><br/>
    <input type="text" value="Submit" />
    </form>
    <script src="https://www.google.com/recaptcha/api.js?render=<?php echo SITE_KEY?>"></script>
    <script>
    grecaptcha.ready(function() {
    grecaptcha.execute('<?php echo SITE_KEY;?>', {action: 'homepage'}).then(function(token) {
    //console.log(token);
    document.getElementById('g-recaptcha-response').value=token;
    });
    });
    </script>
     
    </body>
    </html>

4) Убираем иконку:

Добавляем в форму код со ссылкой на "Лицензионное соглашение" Гугла:

<p style="font-size: 10px;color: gray;">This site is protected by reCAPTCHA and the Google

<a href="https://policies.google.com/privacy">Privacy Policy</a>and <a href="https://policies.google.com/terms">Terms of Service</a> apply.</p>
 

Добавляем в CSS стили темы:

 .grecaptcha-badge { visibility: hidden; } 

Поделиться статьей: