php 谷歌reCAPTCHA v3

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 谷歌reCAPTCHA v3相关的知识,希望对你有一定的参考价值。

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <title>Google reCAPTCHA</title>
  </head>
  <body>

  <?php include 'includes/form-settings.php'; ?>
    
    <div class="container">
        <div class="row">
            <div class="col col-6 offset-md-3">
                <h1 class="text-primary font-weight-light">Google reCAPTCHA</h1>
                <?php if( isset($statusMsg) && !empty($statusMsg) ): echo "<p>{$statusMsg}</p>"; endif; ?>
                <form action="" method="POST" autocomplete="off">
                    <div class="form-row">
                        <div class="col">
                            <label for="first_name">First Name</label>
                            <input type="text" class="form-control" id="first_name" name="first_name" placeholder="First name" required>
                        </div>
                        <div class="col">
                            <label for="last_name">Last Name</label>
                            <input type="text" class="form-control" id="last_name" name="last_name" placeholder="Last name" required>
                        </div>
                    </div>
                    <div class="form-group mt-3">
                        <label for="email">Email address</label>
                        <input type="email" class="form-control" id="email" name="email" placeholder="Email address" required>
                    </div>
                    <div class="form-group mt-3">
                    
                        <!-- Google reCAPTCHA widget -->
                        <div class="g-recaptcha" data-sitekey="6LdwfbUUAAAAAFrBqzqLX2jemOCs5dNeGsmOTrgc" data-size="invisible" data-callback="setResponse"></div>
                        <input type="hidden" id="captcha-response" name="captcha-response" />
    
                        <button type="submit" name="submit" class="btn btn-primary btn-block">Submit</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- JavaScript -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback" async defer></script>
    <script>
        var onloadCallback = function() {
            grecaptcha.execute();
        };
        function setResponse(response) { 
            document.getElementById('captcha-response').value = response; 
            console.log(response);
        }
    </script>

  </body>
</html>
<?php 

$statusMsg = '';
if( isset($_POST['submit']) ):

    $fields = [
        'first_name',
        'last_name',
        'email'
    ];

    $data = array();
    foreach($fields as $field):
        $$field = $_POST[$field];
        $data[$field] = $$field;
    endforeach;

    // var_dump($data);

    // Google reCaptcha secret key
    $secretKey = "6LdwfbUUAAAAAL4Qhb_1blELlh6Fx37KstN8pUlb";

    if( isset($_POST['captcha-response']) && !empty($_POST['captcha-response']) ):
        // Get verify response data
        $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secretKey.'&response='.$_POST['captcha-response']);
        $responseData = json_decode($verifyResponse);
        print_r($responseData);
        if( $responseData->success ):
            //Contact form submission code goes here ...
            $statusMsg = 'Your contact request have submitted successfully.';
        else:
            $statusMsg = 'Robot verification failed, please try again.';
        endif;
    else:
        $statusMsg = 'Robot verification failed, please try again.';
    endif;

endif;

以上是关于php 谷歌reCAPTCHA v3的主要内容,如果未能解决你的问题,请参考以下文章

如何解决 Google v3 reCaptcha 超时?

Spring Boot 下使用谷歌 reCAPTCHA v3

如何在服务器端验证 Google reCAPTCHA v3?

Recaptcha v3 不工作 - 表单不提交

reCAPTCHA v3 - 错误:没有reCAPTCHA客户端

Google reCaptcha v3 与 google reCaptcha Enterprise