2014-12-29 172 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> 
相关问题