【问题标题】:PHP - Validate the recaptcha v2 responsePHP - 验证 recaptcha v2 响应
【发布时间】:2015-11-05 15:30:13
【问题描述】:
#form.php

if(isset($_POST['g-recaptcha-response'])){
    $captcha=$_POST['g-recaptcha-response'];
    $captcha=$_GET["g-recaptcha-response"];
    $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=__1234__&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);

    echo $response;

返回:

注意:未定义索引:g-recaptcha-response 在 /var/www/clients/qmax/app/controllers/job_application_controller.php 第 114 行 { "success": false, "error-codes": [ "missing-输入响应"] }

echo $response->success;

返回:

注意:试图在第 119 行的 /var/www/clients/qmax/app/controllers/job_application_controller.php 中获取非对象的属性

我只需要获取“成功”对象,然后我可以测试它是真还是假,我就完成了。

【问题讨论】:

  • 试试var_dump($_POST)检查你要找的键值是否存在
  • 回声 $response;返回 { "success": false, "error-codes": [ "missing-input-response" ] } 所以字符串为空,使响应为假。我需要获得“成功”值。
  • 这不是google php libairy提供的默认,你试过了吗
  • recaptcha v2 没有提供我知道的 php 库...???
  • 你不应该在互联网上发布你的秘密

标签: php forms validation recaptcha


【解决方案1】:

删除这一行:

$captcha = $_GET["g-recaptcha-response"];

然后你需要像这样通过谷歌解码 json:

$g_response = json_decode($response);

然后只需检查 if/else:

if ($g_response->success === true) echo "success!";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    • 2019-09-10
    • 1970-01-01
    相关资源
    最近更新 更多