2014-12-29 115 views
1

我正在用reCAPTCHA檢查構建一個帶有窗體的網站。根據Google documentation的要求,我爲目標域創建了一個密鑰。然後,我創建了一個包含驗證碼段ReCAPTCHA在本地主機上不工作

HTML表單

<form method="post" action="index.php"> 
    <div class="g-recaptcha" data-sitekey="PUBLIC_KEY"></div> 
    <input type="submit" name="submit" /> 
</form> 
形式


PHP應答確認

當提交表單時,驗證碼的響應進行驗證(在這個例子中,它只是打印)。

$recaptcha = filter_input(INPUT_POST, 'g-recaptcha-response', FILTER_SANITIZE_STRING); 
$googleurl = "https://www.google.com/recaptcha/api/siteverify"; 
$privatekey = "PRIVATE_KEY"; 
$remoteip = $_SERVER['REMOTE_ADDR']; 

$curl = new Curl($googleurl."?secret=".$privatekey."&response=".$recaptcha."&remoteip=".$remoteip); 
$response = json_decode($curl->exec(), true); 

print_r($response); 
die(); 

curl是一個簡單地構建一個curl請求並返回結果的類。

問題

代碼段工作正常在線和我檢查$response都與成功和錯誤的情況下的值。但在開發過程中,我也必須在本地主機上使用它。如this post所述,所有密鑰都應該在本地工作。但是當我運行代碼時,什麼都沒有顯示。

回答

1

雖然這個問題比較老,但我發表的答案是因爲很多人可能遇到同樣的問題。我認爲這可能與本地主機上運行的reCAPTCHA可使用secure token

I posted the solution here for reference

更新來解決涉及到一般的認證問題 - 工作代碼:

對於安全令牌生成I」中號使用slushie's php implementation

PHP的部分:

<?PHP 

use ReCaptchaSecureToken\ReCaptchaToken as ReCaptchaToken; 
require_once("libs/ReCaptchaToken.php"); 

//Generate recaptcha token 
$config = [ 'site_key'  => 'place-your-site-key-here', 
      'site_secret' => 'place-your-secret-key-here' 
      ]; 
$recaptcha_token = new ReCaptchaToken($config); 
$recaptcha_session_id = uniqid('recaptcha'); 
$recaptcha_secure_token = $recaptcha_token->secureToken($recaptcha_session_id); 

?> 

HTML:

<html> 
    <head> 
    ... 
    <script src='//www.google.com/recaptcha/api.js'></script> 
    </head> 
    <body> 
    <form> 
    ... 
    <div class="g-recaptcha" data-sitekey="place-your-site-key-here" data-stoken="<?PHP echo $recaptcha_secure_token; ?>"></div> 
    </form> 
    </body> 
</html> 
相關問題