在我的 index.php 页面中,我在标题中包含了 Recaptcha 脚本:
<script type="text/javascript" src="http://www.google.com/recaptcha/api/js/recaptcha_ajax.js"></script>
在我的 index.php 页面中,我使用以下方法加载我的 homeindex.php:
function loadHome() {
$('#homedynamic').load('php/homeindex.php');
}
现在我的 homeindex.php 页面包含以下代码(简化):
<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>
我有一个 aanmelden.php 页面:
<?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);
}
?>
所以我检查了验证码显示,输入值正确传递到 aanmelden.php 页面......但它没有返回任何东西。我的 aanmelden.php 页面工作正常。我成功使用了函数returnResult。我唯一注意到的是,如果我评论这些行:
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
它确实为我的其他检查返回了状态和 omschrijving。但是随着这些线路的活动,它不知何故不起作用......
有人知道为什么吗?此外,recaptchalib.php 位于正确的位置...