2012-09-21 79 views
1

大家好我正在使用reCaptcha在我的頁面中的聯繫頁面。我想在使用PHP提交表單之前檢查單詞。所以我在Ajax中完成了。這是我的代碼。PHP reCaptcha 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(challengeField = $( 「#輸入recaptcha_challenge_field」)VAL();)我得到不正確。它看起來像這樣

03AHJ_Vut2oHufxF2RvOtWbL7PDrlbU0bj3OgVpxaRg0Ae6-_xjqcIxRJTqD-bnUdTwyeLepickvVC7XLe5kqoWLu1sYdx7ZlmVkEP5TrHA9jGH75kW0Wua6faimq4aRZ5RaOOI0KOoqD0gVOGfeRQ1fw5c5lw-l5WXQ 
+0

您確定這不是一個加密數據嗎?也許有沒有錯誤..並且recaptcha已經加密了正在顯示給你的文字 –

+0

我不太確定..任何其他方式來顯示文字。 – Rakesh

回答

1

綁定在嘗試使用此驗證碼爲整合你的PHP網站

只需使用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! 
} 
+0

任何其他方式在ajax ..? – Rakesh

+0

一般使用這個,因爲它與php5兼容。有其他的方式,但我不以其他方式 –