繁体   English   中英

PHP - 验证 recaptcha v2 响应

[英]PHP - Validate the recaptcha v2 response

#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;

返回:

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

echo $response->success;

返回:

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

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

删除这一行:

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

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

$g_response = json_decode($response);

然后只需检查 if/else:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM