weixin_33716941 2013-08-20 18:28 采纳率: 0%
浏览 28

使用AJAX和PHP的Recaptcha

In my index.php page I include the Recaptcha script in my header:

<script type="text/javascript" src="http://www.google.com/recaptcha/api/js/recaptcha_ajax.js"></script>

And in my index.php page I load my homeindex.php using:

function loadHome() {
    $('#homedynamic').load('php/homeindex.php');
}

Now my homeindex.php page contains the following code (simplified):

<script>
$(document).ready(function() {
    Recaptcha.create("6LfqWeYSAAAAAFStYfL9gsCJ5BFWO60sn4CKbwjj", recaptcha_div, {
        theme: "clean",
        callback: Recaptcha.focus_response_field});
});
</script>

<script>
    function aanmelden() {
        vcaptcha = $("#recaptcha_response_field").val();
        vchallenge = $("#recaptcha_challenge_field").val();

         $.ajax({
            type: "POST",
            dataType: "json",
            data: { captcha:vcaptcha, challenge:vchallenge }, 
            url: './query/aanmelden/aanmelden.php',
            success: function(result) {
                if (result.status == 0) {
                    alert (result.omschrijving);
                }
                if (result.status == 1) {
                    //success
                }
            }
        });
    }

</script>

<div class="right">
        <div id="recaptcha_div"></div>
        <div><button id="aanmelden" type="submit" onClick="aanmelden()">X</button>
        </div>

</div>

And I've got an aanmelden.php page:

<?php

function returnResult($status, $omschrijving){
    $result = array(
        'status' => $status,
        'omschrijving' => $omschrijving
    );
    echo json_encode($result);
    exit();
}

require_once 'recaptchalib.php';
require_once '../../php/connect.php';

$privatekey = "6LfqWeYSAAAAAEH6OrMYD9qJ0SfcWkePTPi99CrZ";
$resp = recaptcha_check_answer ($privatekey,
                            $_SERVER["REMOTE_ADDR"],
                            $_POST["recaptcha_challenge_field"],
                            $_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
    $status = 0;
    $omschrijving = "Captcha is niet correct";
    returnResult($status, $omschrijving);
} else {
    $status = 1;
    $omschrijving = "Captcha is correct";
    returnResult($status, $omschrijving);
}

?>

So I checked that the captcha shows, the input values are passed correctly to the aanmelden.php page... But it doesn't return anything back. My aanmelden.php page works fine. I used the function returnResult with success. The only thing I noticed is that if I comment these lines:

$resp = recaptcha_check_answer ($privatekey,
                            $_SERVER["REMOTE_ADDR"],
                            $_POST["recaptcha_challenge_field"],
                            $_POST["recaptcha_response_field"]);

It does return a status and omschrijving for my other checks. But with these lines active, it somehow doesn't work...

Anybody knows why? Also, recaptchalib.php is on the right location...

  • 写回答

2条回答 默认 最新

  • weixin_33698043 2013-08-21 16:12
    关注

    I got it working!

    Somehow, I made the mistake of using the wrong POST[] field. And I also added Recaptcha.reload(); if a user does make a mistake.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!