php - 将 google reCaptcha 集成到现有的付款表单中

标签 php forms captcha recaptcha

最近我的网站通过我的付款表单收到了大量垃圾邮件,我决定我需要添加一个 captcha 以防止这种情况发生。

我在考虑几个选项后决定使用 Google 的 reCaptcha。设置和使用似乎很容易,但我遇到了一些问题。

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

<script src='https://www.google.com/recaptcha/api.js'></script>

然后我将实际的 captcha 本身包含在表单的底部:

<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 中,成功将是 true 或 false,如果是 true 则我想继续付款,如果是 false 则停止并返回表单。

非常感谢任何帮助。或者,如果有人有添加验证码的替代解决方案,我愿意研究一下。

最佳答案

尝试运行 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)";
    }
}

关于php - 将 google reCaptcha 集成到现有的付款表单中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27126154/

相关文章:

php - 使用 jQuery 调用 json 时搜索不起作用

php - Symfony2 中的 HTML 表单标签而不是纯文本

php - 动态类别的数据库架构

javascript - 需要使用 javascript trim 表单中的所有元素

php - 无法访问 Live Server 中的 codeigniter Controller ,但可以在本地主机中访问(Windows 上的 xampp)

javascript - 提交时检查表单字段更改

jquery - 管理大量 Web 表单

azure - 机器人保护或添加验证码以注册 Azure AD B2C 体验

css - 验证码 CSS 样式覆盖

javascript - 使用 Google Invisible Captcha 自动 html 表单验证和必需属性时不起作用。为什么?