php - 新的 Google ReCaptcha 实现和提交前检查

标签 php jquery google-api recaptcha

我是 PHP、jQuery 和 AJAX 新手。

我正在尝试实现新的 Google Recapcha。

看起来像 -

点击后- 1

经过验证- 2

3

index.php 的代码是-

<html>
<head>
    <title>Google recapcha demo - Codeforgeek</title>
</head>
<body>
    <h1>Google reCAPTHA Demo</h1>
    <form id="comment_form" action="received.php" method="post">
        <input type="email" placeholder="Type your email" size="40"><br><br>
        <textarea name="comment" rows="8" cols="39"></textarea><br><br>
        <!-- ---------------------------------------Capcha Start------------------------------------- -->
            <script src='https://www.google.com/recaptcha/api.js'></script>
            <?php $siteKey="6LdLqv8SAAAAADT3YEjm6ONCwnPD95frMSZ92Ftv" ?>
            <div class="g-recaptcha" data-sitekey="<?php echo $siteKey; ?>"></div>
        <!-- ----------------------------------------Capcha End------------------------------------ -->
        <br><br>
        <input type="submit" name="submit" value="Post comment"><br><br>
    </form>
</body>
</html>

以及收到的部分-received.php

<?php
//////////////////////////////////////////Check Capch Function Start
function CapchaCheck()
{
    $captcha;
    if(isset($_POST['g-recaptcha-response']))
    {
        $captcha=$_POST['g-recaptcha-response'];
    }
    if(!$captcha)
    {
        return false;
    }
    $secreatKey="6LdLqv8SAAAAAIWxKcn2zIKjWau2Mdz6yzE3Kkcm";
    $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secreatKey."&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
    var_dump($response);
    if($response.success==false)
    {
        return false;
    }
    else
    {
        return true;
    }
}
//////////////////////////////////////////Check ReCaptcha Function End

if(CapchaCheck())
{
    echo '<h2>Thanks for posting comment.</h2>';
}
else
{
    echo '<h2>You are spammer ! Get the @$%K out</h2>';
}
?>

它工作得很好。

但我不想在提交后检查 ReCapcha 是否正确。如果 ReCaptcha 错误,我想阻止用户提交。

所以,我想我需要 jQuery。

但我不知道如何实现。

谁能帮帮我。

预先感谢您的帮助。

最佳答案

function reCaptchaServerControl(){ 
var reCaptchaResponse = $("#g-recaptcha-response").val();
if (reCaptchaResponse) {
    $.ajax({
        type: 'POST',
        url: "submitContact.php", 
        dataType: 'html',
        async: true,
        data: {response : reCaptchaResponse} ,
        success: function(serverResponse) { 
            //alert(serverResponse);
            var serverResponse = $.parseJSON(serverResponse);
          },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                //alert("Error");
            }
        });
    } else {
        //alert("error");
    }    
}

关于php - 新的 Google ReCaptcha 实现和提交前检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27686269/

相关文章:

javascript - 未知 "BESbswy"<跨度>

php - Symfony 2 - 从数据库加载角色

php - 使用 DynamoDB 和 MySQL 进行地理空间查询

php - 当列具有相同名称时如何左连接

javascript - 简单的jquery插件,快速查看信息

google-api - 每次请求访问应用程序时,Google auth2 都会请求许可

php - NuSOAP 和内容类型

javascript - 同一页面上某些 JavaScript 和 jQuery 之间的冲突

java - 用于过滤/排序大量数据的插件或模块?

php - Google Docs 文档选择器?