gpt4 book ai didi

php - 验证码 GET/POST

转载 作者:行者123 更新时间:2023-12-02 20:53:53 30 4
gpt4 key购买 nike

我正在对我已经完成的网络系统部分进行一小部分升级,其中之一是确保我的 Google reCaptcha 的安全性正确。

目前,我使用此代码:

//reCaptcha
$Url = "https://www.google.com/recaptcha/api/siteverify";
$SecretKey = "----Secret Key----";
$Response = file_get_contents($Url."?secret=".$SecretKey."&response=".$_POST['Response']);
$Robot = json_decode($Response);

这工作正常,但是 Google 的文档说你应该使用 POST 方法而不是 get 方法,显然是为了确保有人不会获得我的 key 。但是,我不确定如何执行此操作,因此我们将不胜感激。我知道我可能必须使用 cURL,但是,我对此一无所知,并且不确定如何安装它(如果需要)。

谢谢,汤姆。

最佳答案

... POST the variables to Google's reCaptcha server instead of sending them via GET.

$Response = file_get_contents($Url."?secret=".$SecretKey."&response=".$_POST['Response']);

如果您想通过 HTTP POST 将数据发送到 Google 服务器,则必须使用客户端 URL,而不是将数据嵌入到 URL 中(如上述 URL 中的 key 和响应)并通过 GET 发送。图书馆。

引用资料如下:

您的服务器端 PHP 代码应如下所示:

$Url = "https://www.google.com/recaptcha/api/siteverify";
$SecretKey = "----Secret Key----";
if(isset($_POST['Response']) && !empty($_POST['Response'])){
//get verified response data
$data = array('secret' => $secret, 'response' => $_POST['Response']);

$ch = curl_init($Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$verifyResponse = curl_exec($ch);
curl_close($ch);

$responseData = json_decode($verifyResponse);

// your code

}else{
echo "Please click on the reCAPTCHA box.";
}

这里有几点需要注意,

  • CURLOPT_RETURNTRANSFER 设置为 true 可以将传输作为 curl_exec() 返回值的字符串返回,而不是直接输出。
  • CURLOPT_SSL_VERIFYPEER 可用于验证对等方的证书。如果我们将其指定为false,它将接受任何服务器(对等)证书。
  • CURLOPT_POST 用于执行常规 HTTP POST。此 POST 是普通的 application/x-www-form-urlencoded 类型,最常用于 HTML 表单。
  • CURLOPT_POSTFIELDS 用于指定我们要通过此 POST 请求提交的完整数据。 $data 数组应使用 http_build_query() 转换为 URL 编码的查询字符串。函数,以便它可以作为 application/x-www-form-urlencoded 发送。

关于php - 验证码 GET/POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41128779/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com