Php 将google reCaptcha集成到现有支付表单中

Php 将google reCaptcha集成到现有支付表单中,php,forms,captcha,recaptcha,Php,Forms,Captcha,Recaptcha,最近,我的网站通过我的支付表单收到了很多垃圾邮件,我决定我需要添加一个captcha,以防止出现这种情况 我在考虑一些选择,我决定选择谷歌reCaptcha。它似乎很容易设置和使用,但我遇到了一些问题 首先,我已将此脚本包含在表单的标题中: <script src='https://www.google.com/recaptcha/api.js'></script> <div class="g-recaptcha" data-sitekey="6LdOVv4SAA

最近,我的网站通过我的支付表单收到了很多垃圾邮件,我决定我需要添加一个
captcha
,以防止出现这种情况

我在考虑一些选择,我决定选择谷歌
reCaptcha
。它似乎很容易设置和使用,但我遇到了一些问题

首先,我已将此
脚本
包含在表单的标题中:

<script src='https://www.google.com/recaptcha/api.js'></script>
<div class="g-recaptcha" data-sitekey="6LdOVv4SAAAAAJ4muJvo_vD7vsd9T9QIfkEwcO7y"></div>
提交表格时,我会执行以下操作:

$captcha = $_POST["g-recaptcha-response"]; //Get Captcha token
$secret_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; //Get Secret key
$google_response = http_get("https://www.google.com/recaptcha/api/siteverify", array("secret"=>$secret, "response"=>$captcha), $info); //perform Get request
print_r($info);
但是什么也没有发生,事实上,以前工作的页面只是挂起,甚至没有显示错误消息。你知道我做错了什么吗?根据我对文档的理解,响应将在
JSON
中,成功将是真或假,如果是真,我希望继续付款,如果是假,我希望停止并返回表单


非常感谢您的帮助。或者,如果有人有另一种添加验证码的解决方案,我愿意对此进行研究

尝试运行google new recaptcha 2015:

==PHP Verification==
if(isset($_POST['submit'])){
    $userIP = $_SERVER["REMOTE_ADDR"];
    $recaptchaResponse = $_POST['g-recaptcha-response'];
    $secretKey = "SECRET_KEY";
    $request = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secretKey}&response={$recaptchaResponse}&remoteip={$userIP}");

    if(!strstr($request, "true")){
        echo "NOPE (Failed Verification)";
    }
    else{
        echo "YUP (Successful Verification)";
    }
}

不要使用文件获取内容。谷歌调用他们的api。上述GET请求可能会导致多个问题:

  • 由于安全原因,调用url可能被阻止(允许url打开)
  • 用户可能会将类似someToken&secret=otherSitesSecret的内容作为输入传递给您的$\u POST[g-recaptcha-response]。如果您只需在被调用的url处输入字符串,您将向Google传递另一个秘密。然后,用户可以使用从任何站点获得的任何recaptcha响应来验证自己。这是一个安全问题
  • 谷歌返回的代币可能相当长。Web服务器只支持URL中的几千个字符。这可能会导致字符串被截断,您将收到有效输入的错误
因此,使用类似的方法:

// Get resource
$curl = curl_init();

// Configure options, incl. post-variables to send.
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'https://www.google.com/recaptcha/api/siteverify',
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array(
        'secret' => 'your_secret_code_here',
        'response' => $_POST['g-recaptcha-response']
    )
));

// Send request. Due to CURLOPT_RETURNTRANSFER, this will return reply as string.
$resp = curl_exec($curl);

// Free resources.
curl_close($curl);

// Validate response
if(strpos($resp, '"success": true') !== FALSE) {
    echo "Verified.";
} else {
    echo "Not verified.";
}

此外,验证部分在其接受为“已验证”的内容方面更为保守

无法轻松地将响应解析回PHP数组或JSON对象,这非常令人恼火。@NicholasPickering如果您使用的是Composer,这是一个很好的软件包。Google itselfs建议仅使用POST请求进行验证。如果google不阻止多个机密参数,则您的方法容易受到用户的攻击,这些用户将“token&secret=secretofanotherrepactchaaccount”作为g-repactcha-response。。这意味着用户可以使用任何有效的recaptcha令牌进行验证,这是错误的。最好使用curl和post请求。根据,如果您没有用于调用的SSL证书(我想这不是实现安全功能的最安全方式!),那么您希望在阵列中添加
CURLOPT\u SSL\u VERIFYPEER=>0
?Google总是为他们的RecapTCHAAPI使用有效的证书。如果您的系统未验证,则系统出现问题。我还评论了你链接的答案,说真的没有必要这样做。只是为了开发目的(如果你没有设置ssl证书)