2013-08-20 51 views
2

在我的index.php頁面我包括我的頭的ReCaptcha腳本:的Recaptcha使用AJAX和PHP

<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 p年齡:

<?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是在正確的位置...

回答

0

我得到它的工作!

不知何故,我犯了使用錯誤的POST []字段的錯誤。 而且我還添加了Recaptcha.reload();如果用戶確實犯了錯誤。

1

你應該不惜任何代價隱藏你的祕密價值(你暴露在你的問題)。

任何人都有這個信息可以黑客您的驗證碼,容易

如果您打算在公共網站上使用此驗證,我建議您在google recaptcha page處重新創建它。