1

大家好,我在我的页面中使用 reCaptcha,它是一个联系页面。我想在用 PHP 提交表单之前检查单词。所以我在 Ajax 中完成了它。这是我的代码。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Re captcha</title>
<script type="text/javascript" src="jquery-1.2.3.js"></script>
<script type="text/javascript">
function validateCaptcha()
{

challengeField =  $("input#recaptcha_challenge_field").val();
responseField = $("input#recaptcha_response_field").val();

alert(challengeField);
alert(responseField);
//return false;
var html = $.ajax({
type: "POST",
url: "ajax.recaptcha.php",
data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
async: false
}).responseText;

if(html == "success")
{
   // $("#captchaStatus").html("Success. Submitting form.");
   // return false;
    // Uncomment the following line in your application
    //load_ajax();
    alert("Captcha success..");
    return true;
}
else
{
    $("#captchaStatus").html("Image verification failed);
    Recaptcha.reload();
    return false;
}
}
</script>

</script>
</head>

<body>
<div style="height:300px;width:350px;margin:auto;background:#969;padding:20px;>
<form action = "javascript:void(null);" name="frmSubmit" method="post" onsubmit="return validateCaptcha();">
<label>Name</label><br />
<input type="text" name="txtName" id="NameTextBox" />
<br />
<label>E Mail</label><br />
<input type="text" name="txtEmail" id="EmailTextBox" />
<br />
<br />
<?php

 require_once('captcha/recaptchalib.php');

 // Get a key from https://www.google.com/recaptcha/admin/create

 //Localhost

  $publickey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  $privatekey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";



  # the response from reCAPTCHA
  $resp = null;
  # the error code from reCAPTCHA, if any
  $error = null;

  # was there a reCAPTCHA response?
   if ($_POST["recaptcha_response_field"]) {
    $resp = recaptcha_check_answer ($privatekey,
                                    $_SERVER["REMOTE_ADDR"],
                                    $_POST["recaptcha_challenge_field"],
                                    $_POST["recaptcha_response_field"]);

    if ($resp->is_valid) {
            echo "You got it!";
    } else {
            # set the error code so that we can display it
            $error = $resp->error;
    }
    }
    echo recaptcha_get_html($publickey, $error);
     ?>
     <br />
    <input   name="BtnSubmit"type="submit"onclick="MM_validateForm('NameTextBox','','R','EmailTextBox','','R');return document.MM_returnValue" value="Send" />

    </form>
    </div>
     <script type="text/javascript">
            var RecaptchaOptions = {
                theme : 'Red'
            };
        </script>
       </body>
       </html>

我得到的错误是我得到的 chlenge filed(challengeField = $("input#recaptcha_challenge_field").val();) 不正确。它看起来像这样

03AHJ_Vut2oHufxF2RvOtWbL7PDrlbU0bj3OgVpxaRg0Ae6-_xjqcIxRJTqD-bnUdTwyeLepickvVC7XLe5kqoWLu1sYdx7ZlmVkEP5TrHA9jGH75kW0Wua6faimq4aRZ5RaOOI0KOoqD0gVOGfeRQ1fw5c5lw-l5WXQ
4

1 回答 1

1

绑定尝试在您的 PHP 站点上使用它进行 recaptcha 集成

只需使用Zend_Service_ReCaptcha。您只需几行代码即可集成此服务:

//Creating instance
$recaptcha = new Zend_Service_ReCaptcha($pubKey, $privKey);

//Display output
echo $recaptcha->getHTML();

//Handling input
$result = $recaptcha->verify(
    $_POST['recaptcha_challenge_field'],
    $_POST['recaptcha_response_field']
);

//And finally validate captcha
if ($result->isValid()) {
    //Cool!
}
于 2012-09-21T09:38:21.867 回答