Php 使用jquery AJAX进行Google-reCaptcha验证

Php 使用jquery AJAX进行Google-reCaptcha验证,php,jquery,ajax,forms,recaptcha,Php,Jquery,Ajax,Forms,Recaptcha,我不知道如何将此应用到我的登录页面,一旦ajax上的验证码成功响应,然后提交表单。 这是我的html表单(我将操作保留为空,因为我仍在测试中) 我的captcha.php我如何收到$\u POST['captcha'] <?php if($_POST['captcha']){ $url = 'https://www.google.com/recaptcha/api/siteverify'; $privatekey = 'MY_SECRET_KEY';

我不知道如何将此应用到我的登录页面,一旦ajax上的验证码成功响应,然后提交表单。 这是我的html表单(我将操作保留为空,因为我仍在测试中)

我的
captcha.php
我如何收到
$\u POST['captcha']

<?php
if($_POST['captcha']){
        $url = 'https://www.google.com/recaptcha/api/siteverify';
        $privatekey = 'MY_SECRET_KEY';

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

        if($data->sucess==true){
            echo "success";
        }
        else{
            echo "failed";
        }
    }
?>

请帮助我了解它是如何工作的,以及如何使用AJAX来完成 提前感谢:)

更新

我只是注意到我怎样才能
$\u POST['g-recaptcha-response']??

您可以使用以下代码:

HTML代码:

<form action="" method="POST" id="loginForm">
    <input type="text" id = "email"  name="email">
    <input type="password" id="pass" name="password">
    <textarea type="text" name="message"></textarea>
    <div class="g-recaptcha" data-sitekey="10LDDpf0ehtMZY6kdJnGhsYYY-6ksd-W"></div>
    <input type="submit" name="submit" value="SUBMIT">
</form>
<?php
if(isset($_POST['submit']) && !empty($_POST['submit'])):
    if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
        //your site secret key
        $secret = '10LDDpf0ehtMZY6kdJnGhsYYY';
        //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):
            //captacha validated successfully.
            $email = !empty($_POST['email'])?$_POST['email']:'';
            $password = !empty($_POST['password'])?$_POST['password']:'';
            echo "captacha validated successfully.";
        else:
            echo "Robot verification failed, please try again.";
        endif;
    else:
         echo 'invalid captcha';
    endif;
else:
   //Nothing
endif;
?>
PHP代码:

<form action="" method="POST" id="loginForm">
    <input type="text" id = "email"  name="email">
    <input type="password" id="pass" name="password">
    <textarea type="text" name="message"></textarea>
    <div class="g-recaptcha" data-sitekey="10LDDpf0ehtMZY6kdJnGhsYYY-6ksd-W"></div>
    <input type="submit" name="submit" value="SUBMIT">
</form>
<?php
if(isset($_POST['submit']) && !empty($_POST['submit'])):
    if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
        //your site secret key
        $secret = '10LDDpf0ehtMZY6kdJnGhsYYY';
        //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):
            //captacha validated successfully.
            $email = !empty($_POST['email'])?$_POST['email']:'';
            $password = !empty($_POST['password'])?$_POST['password']:'';
            echo "captacha validated successfully.";
        else:
            echo "Robot verification failed, please try again.";
        endif;
    else:
         echo 'invalid captcha';
    endif;
else:
   //Nothing
endif;
?>

我正在使用jQuery/ajax进行重新验证码验证,如下所示:

<script src="https://www.google.com/recaptcha/api.js" >;
<form method="post" name="contactForm">
    <input type="text" name="fname"/>
    <input type="text" name="lname"/>
    <input type="text" name="Phone"/>
    <div class="g-recaptcha" data-sitekey="[site_key]" data-callback="onReturnCallback" data-theme="dark"></div>
    <input value="submit" type="submit"/>
</form> 
验证/ajax:

//Initialize jQuery 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script type="text/javascript">
    var onReturnCallback = function(response) { 
      var url='proxy.php?url=' + 'https://www.google.com/recaptcha/api/siteverify';  
        $.ajax({ 'url' : url, 
                   dataType: 'json',
                   data: { response: response},
                   success: function(result) {                     
                    var res = result.success.toString();
                            alert(res);                    
                    if (res ==  'true') { 
                           document.getElementById('g-recaptcha').innerHTML = ' Your Success Message'; 
                                    } 
                               } 
             });
    }; 
    </script>
//初始化jQuery
var onReturnCallback=函数(响应){
var url='proxy.php?url='+'https://www.google.com/recaptcha/api/siteverify';  
$.ajax({'url':url,
数据类型:“json”,
数据:{response:response},
成功:函数(结果){
var res=result.success.toString();
警报(res);
如果(res='true'){
document.getElementById('g-recaptcha')。innerHTML='Your Success Message';
} 
} 
});
}; 

请参见SO上的类似问题。如果用户关闭JS怎么办?
//Initialize jQuery 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script type="text/javascript">
    var onReturnCallback = function(response) { 
      var url='proxy.php?url=' + 'https://www.google.com/recaptcha/api/siteverify';  
        $.ajax({ 'url' : url, 
                   dataType: 'json',
                   data: { response: response},
                   success: function(result) {                     
                    var res = result.success.toString();
                            alert(res);                    
                    if (res ==  'true') { 
                           document.getElementById('g-recaptcha').innerHTML = ' Your Success Message'; 
                                    } 
                               } 
             });
    }; 
    </script>