Codeigniter Google Recaptcha表单验证无法在服务器中运行

t30tvxxf  于 2022-12-07  发布在  Go
关注(0)|答案(1)|浏览(109)

我在其中一个页面中使用了Google Recaptcha表单验证。代码在本地计算机中工作正常。我在服务器中上传了相同的代码,它在服务器中给出了以下错误。

我检查了php.ini文件中的allow_url_fopen值,该值为true。我的工作代码是:

$secret ="sercret key";
$ip = $_SERVER['REMOTE_ADDR'];
$captcha = $_POST['g-recaptcha-response'];
$rsp = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$captcha&remoteip$ip");

$arr = json_decode($rsp , true);
dl5txlt9

dl5txlt91#

我想我回复这个有点晚了。但是没关系,我可能可以帮助那些需要我帮助的人。
有两种方法可以解决这个问题。
方法1:(我不推荐你这样做,即使你能从YouTube上找到)

$secret ="sercret key";
$ip = $_SERVER['REMOTE_ADDR'];
$captcha = $_POST['g-recaptcha-response'];
$rsp = file_get_contents("https://www.google.com/recaptcha/api/siteverify? 
secret=".$secret."&response=".$captcha."&remoteip=".$ip);
$arr = json_decode($rsp , true);

方法2:一种更好的方法是使用cURL。

$secret ="sercret key";
$ip = $_SERVER['REMOTE_ADDR'];
$captcha = $_POST['g-recaptcha-response'];
$rsp = "https://www.google.com/recaptcha/api/siteverify? 
secret=".$secret."&response=".$captcha."&remoteip=".$ip;

        $ch = curl_init();   
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   
        curl_setopt($ch, CURLOPT_URL, $rsp);   
        $apiresponse = curl_exec($ch);
        $apiresponse = json_decode($apiresponse,true);

相关问题