Firebase 项目:访问 Firebase 控制台,创建一个新项目,并获取 API 密钥。
PHP 环境搭建:确保本地或服务器环境已正确安装 PHP 及相关扩展。
<?php
require_once('path/to/firebase/autoload.php');
use Kreait\Firebase\Factory;
// 初始化 Firebase 实例
$firebase = (new Factory)
->withServiceAccount('path/to/firebase/serviceAccountKey.json')
->create();
若需设置 API 密钥,可以进一步扩展配置:
<?php
$firebase = (new Factory)
->withServiceAccount('path/to/firebase/serviceAccountKey.json')
->withApiKey('your-api-key')
->create();
首先,在 Firebase 控制台启用 reCAPTCHA 并获取对应的 site key。接着,在后端中实例化验证码验证器:
<?php
$recaptcha = $firebase->getAuth()->getRecaptchaVerifier([
'siteKey' => 'your-site-key',
]);
<html>
<body>
<form>
<!-- 添加 reCAPTCHA 验证区域 -->
<div id="recaptcha-container"></div>
<button type="submit">Submit</button>
</form>
<!-- 引入 reCAPTCHA 的 JavaScript 脚本 -->
<script src="https://www.google.com/recaptcha/api.js?render=explicit"></script>
grecaptcha.ready(function() {
grecaptcha.execute('your-site-key', {action: 'homepage'}).then(function(token) {
document.getElementById('recaptcha-token').value = token;
});
});
</script>