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>