#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;
返回:
Notice: Undefined index: g-recaptcha-response in /var/www/clients/qmax/app/controllers/job_application_controller.php on line 114 { "success": false, "error-codes": [ "missing-input-response" ] }
echo $response->success;
返回:
Notice: Trying to get property of non-object in /var/www/clients/qmax/app/controllers/job_application_controller.php on line 119
我只需要获得“成功”对象,然后我就可以测试它是 True 还是 False 就完成了。
最佳答案
删除这一行:
$captcha = $_GET["g-recaptcha-response"];
然后你需要像这样通过谷歌解码json:$g_response = json_decode($response);
然后只需检查 if/else:if ($g_response->success === true) echo "success!";
关于PHP - 验证 recaptcha v2 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31975099/