如何在 html 和 php 中 运行 重新验证 2?

How to run recaptcha 2 in html and php?

索引html:

<!DOCTYPE html>
<html>
<head>
</head>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<body>
<form method="post"  action="/cms/common/sendEmail" enctype="multipart/form-data"  class="valida tc"  id="thisForm">
    <input type="hidden" name="lang" value="tch" />
    <tr>
    <td>
    <div class="g-recaptcha" id="recaptcha" data-sitekey="sitekey"></div>
    </td>
    </tr>
<tr>
<td class="submitBtnWrap"><input type="submit" value="送出" class="roundBtn" /><span class="at-error"></span></td>
</tr>
</table>
</form>
</body>
</html>

recaptcha.php:

<?php
    $public_key = "6Lc9oGIaAAAAAMK6Q4ZZ_qYzlvVCV1nydMLDUUoZ";
    $private_key = "6Lc9oGIaAAAAAEthTaDOcm3VJ9Uldizbl6ZDKQ2_";
    $url = "https://www.google.com/recaptcha/api/siteverify";

    $q

    =$_GET["q"];
        echo $q;
        
        if(array_key_exists('submit_form',$_POST)){
    
            // echo "<pre>";print_r($_POST);echo"</pre>";
            $response_key = $_POST['g-recaptcha-response'];
            $response = file_get_contents($url.'?secret='.$private_key.'&response='.$response_key.'&remoteip='.$_SERVER['REMOTE_ADDR']);
            $response = json_decode($response);
            
            // echo "<pre>";print_r($response);echo "</pre";
    
            if($response->success == 1)
            {
                echo "Your information was valid...";
            }else{
                echo "You are a robot and we don't like robts.";
            }
        }
    
    ?>

您好,我尝试在我的 web.xml 中添加 recaptcha 2。站点端和服务器端都完成了。 但是我如何使用 php 文件将令牌发送到 google 服务器端验证,因为无法将表单操作编辑为 action="recaptcha.php".

或者是否有任何解决方案,例如使用 ajax、javascript 来完成服务器端验证?

请帮助我。谢谢大家

如果您无法更改 action="recaptcha.php",您需要使用 AJAX 来验证回复。

所以,你的 index.html 应该像

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="post"  action="/cms/common/sendEmail" enctype="multipart/form-data"  class="valida tc"  id="thisForm">
    <input type="hidden" name="lang" value="tch" />
    <tr>
    <td>
    <div id="recaptcha" ></div>
    </td>
    </tr>
<tr>
<td class="submitBtnWrap"><input type="submit" value="送出" class="roundBtn" /><span class="at-error"></span></td>
</tr>
</table>
</form>

<!-- Add jquery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<!-- Add recaptcha explicitly -->
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" async defer></script>

<!-- render and verify by ajax -->
<script>

    var onloadCallback = function() {
        grecaptcha.render('recaptcha', {
            'sitekey' : '6Lc9oGIaAAAAAMK6Q4ZZ_qYzlvVCV1nydMLDUUoZ',
            'callback' : verifyCallback,
        });
    }

    var verifyCallback = function(response) {
        $.post('recaptcha.php', {'g-recaptcha-response' : response}, function(data){
            alert(data);
        });
    };
        
</script>
</body>
</html>

你的recaptcha.php:

<?php
    $public_key = "6Lc9oGIaAAAAAMK6Q4ZZ_qYzlvVCV1nydMLDUUoZ";
    $private_key = "6Lc9oGIaAAAAAEthTaDOcm3VJ9Uldizbl6ZDKQ2_";
    $url = "https://www.google.com/recaptcha/api/siteverify";
        
    if(isset($_POST['g-recaptcha-response'])){

        $response_key = $_POST['g-recaptcha-response'];
        $response = file_get_contents($url.'?secret='.$private_key.'&response='.$response_key.'&remoteip='.$_SERVER['REMOTE_ADDR']);
        $response = json_decode($response);

        if($response->success == 1)
        {
            echo "Your information was valid...";
        }else{
            echo "You are a robot and we don't like robts.";
        }
    }
?>