如何在服务器端验证 Google reCAPTCHA v3?

在服务器端使用 Google reCAPTCHA v3
与基于复选框的前身相比,Google 的 reCAPTCHA v3 是一种更先进的机器人检测方法。虽然在前端实现它很简单,但在服务器端处理验证需要不同的方法。
已弃用的 reCAPTCHA v2 验证
您提到的用于reCAPTCHA v2 验证不再适合 v3。 reCAPTCHA v3 使用带有附加参数和密钥的 POST 请求进行验证。
reCAPTCHA v3 的基于 POST 的安全验证
这是使用 POST 修改的 PHP 脚本 -基于 reCAPTCHA v3 的验证:
<code class="php">function isValid() {
try {
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = [
'secret' => '[YOUR SECRET KEY]',
'response' => $_POST['g-recaptcha-response'],
'remoteip' => $_SERVER['REMOTE_ADDR']
];
$options = [
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
]
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return json_decode($result)->success;
} catch (Exception $e) {
return null;
}
}</code>
用法
调用 isValid() 函数来验证 reCAPTCHA 令牌并相应地处理响应。例如:
<code class="php">if (isValid()) {
// The user has passed the reCAPTCHA check.
// ...
} else {
// The user has failed the reCAPTCHA check.
// ...
}</code>
安全注意事项
必须在 POST 请求中使用密钥来保护验证的完整性。请将此密钥保密,切勿公开泄露。
以上是如何在服务器端验证 Google reCAPTCHA v3?的详细内容。更多信息请关注PHP中文网其他相关文章!
热AI工具
Undress AI Tool
免费脱衣服图片
Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片
AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。
Clothoff.io
AI脱衣机
Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!
热门文章
热工具
记事本++7.3.1
好用且免费的代码编辑器
SublimeText3汉化版
中文版,非常好用
禅工作室 13.0.1
功能强大的PHP集成开发环境
Dreamweaver CS6
视觉化网页开发工具
SublimeText3 Mac版
神级代码编辑软件(SublimeText3)
我如何了解最新的PHP开发和最佳实践?
Jun 23, 2025 am 12:56 AM
TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource
什么是PHP,为什么它用于Web开发?
Jun 23, 2025 am 12:55 AM
PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti
如何设置PHP时区?
Jun 25, 2025 am 01:00 AM
tosetTherightTimeZoneInphp,restate_default_timezone_set()functionAtthestArtofyourscriptWithavalIdidentIdentifiersuchas'america/new_york'.1.usedate_default_default_timezone_set_set()
我如何验证PHP中的用户输入以确保其符合某些标准?
Jun 22, 2025 am 01:00 AM
TovalidateuserinputinPHP,usebuilt-invalidationfunctionslikefilter_var()andfilter_input(),applyregularexpressionsforcustomformatssuchasusernamesorphonenumbers,checkdatatypesfornumericvalueslikeageorprice,setlengthlimitsandtrimwhitespacetopreventlayout
编写清洁和可维护的PHP代码的最佳实践是什么?
Jun 24, 2025 am 12:53 AM
写干净、易维护的PHP代码关键在于清晰命名、遵循标准、合理结构、善用注释和可测试性。1.使用明确的变量、函数和类名,如$userData和calculateTotalPrice();2.遵循PSR-12标准统一代码风格;3.按职责拆分代码结构,使用MVC或Laravel式目录组织;4.避免面条式代码,将逻辑拆分为单一职责的小函数;5.在关键处添加注释并撰写接口文档,明确参数、返回值和异常;6.提高可测试性,采用依赖注入、减少全局状态和静态方法。这些做法提升代码质量、协作效率和后期维护便利性。
什么是php(serialize(),Unserialize())中的数据序列化?
Jun 22, 2025 am 01:03 AM
thephpfunctionserize()andunSerialize()redustoconvertComplexdatStructDestoresToroStoroStoroSandaBackagagain.1.Serialize()
如何将PHP代码嵌入HTML文件中?
Jun 22, 2025 am 01:00 AM
可以将PHP代码嵌入HTML文件中,但需确保文件以.php为扩展名,以便服务器能正确解析。使用标准的标签包裹PHP代码,可在HTML中任意位置插入动态内容。此外,可在同一文件中多次切换PHP与HTML,实现条件渲染等动态功能。务必注意服务器配置及语法正确性,避免因短标签、引号错误或遗漏结束标签导致问题。
如何使用PHP执行SQL查询?
Jun 24, 2025 am 12:54 AM
Yes,youcanrunSQLqueriesusingPHP,andtheprocessinvolveschoosingadatabaseextension,connectingtothedatabase,executingqueriessafely,andclosingconnectionswhendone.Todothis,firstchoosebetweenMySQLiorPDO,withPDObeingmoreflexibleduetosupportingmultipledatabas


