在网站表单中使用 Cloudflare Turnstile 主要目的是:
结合 Formspree + AJAX,可以实现 无刷新、快速提交,同时保证安全性和用户体验。
Cloudflare 账号,并在 Turnstile 中注册网站:
登录 Cloudflare 打开 Cloudflare Dashboard。
进入 Turnstile 页面
注册站点
填写:
example.com
获取 Key
前端使用
在表单中添加:
<div class="cf-turnstile" data-sitekey="你的-sitekey"></div>
引入 JS:
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
后端验证
cf-turnstile-response
token后端发送请求到 Cloudflare:
POST https://challenges.cloudflare.com/turnstile/v0/siteverify
secret=你的-secret-key
response=用户token
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
<form id="contact-form" action="submit.php" method="POST" class="container mt-3">
<div class="mb-3">
<input type="text" class="form-control" placeholder="Name" name="name" required>
</div>
<div class="mb-3">
<input type="email" class="form-control" placeholder="E-mail" name="email" required>
</div>
<div class="mb-3">
<textarea class="form-control" placeholder="Enter your message..." rows="6" name="message" required></textarea>
</div>
<!-- Honeypot 防垃圾 -->
<div style="display:none;">
<input type="text" name="_gotcha" tabindex="-1" autocomplete="off">
</div>
<!-- Turnstile 验证 -->
<div class="cf-turnstile mb-3" data-sitekey="你的-sitekey"></div>
<button type="submit" class="btn btn-warning w-100">Send Message</button>
<div id="form-status" class="mt-2"></div>
</form>
<script>
$(function() {
$("#contact-form").on("submit", async function(e){
e.preventDefault();
const $form = $(this);
const status = $("#form-status");
status.html("<p style='color:blue;'>⏳ 提交中,请稍候...</p>");
const data = new FormData(this);
// 获取 Turnstile token
const token = $('input[name="cf-turnstile-response"]').val();
if (!token) {
status.html("<p style='color:red;'>⚠️ 请先完成验证。</p>");
return;
}
data.append("cf-turnstile-response", token);
try {
const response = await fetch($form.attr("action"), {
method: "POST",
body: data
});
const result = await response.json();
if (result.success) {
status.html("<p style='color:green;'>✅ 提交成功,我们会尽快联系您!</p>");
$form[0].reset();
if (window.turnstile) turnstile.reset();
} else {
status.html("<p style='color:red;'>❌ 提交失败,请稍后再试。</p>");
}
} catch (error) {
status.html("<p style='color:red;'>⚠️ 网络错误,请检查连接。</p>");
}
});
});
</script>
<?php
header('Content-Type: application/json');
$secret = "你的-secret-key";
$token = $_POST['cf-turnstile-response'] ?? '';
if (!$token) { echo json_encode(['success'=>false]); exit; }
// 验证 Turnstile
$verify = curl_init();
curl_setopt($verify, CURLOPT_URL, "https://challenges.cloudflare.com/turnstile/v0/siteverify");
curl_setopt($verify, CURLOPT_POST, true);
curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query([
'secret' => $secret,
'response' => $token
]));
curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($verify);
curl_close($verify);
$result = json_decode($response, true);
if (!$result['success']) { echo json_encode(['success'=>false]); exit; }
// 转发到 Formspree
$formspree_url = "https://formspree.io/f/xxxxxx";
$data = [
'name' => $_POST['name'] ?? '',
'email' => $_POST['email'] ?? '',
'message' => $_POST['message'] ?? '',
'_gotcha' => $_POST['_gotcha'] ?? ''
];
$ch = curl_init($formspree_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json']);
curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo json_encode(['success'=>($http_code>=200 && $http_code<300)]);
?>
CSS
Bootstrap 5
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
JS
jQuery(AJAX)
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
Bootstrap JS(可选)
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
Cloudflare Turnstile JS
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
submit.php
Hong Kong、Shenzhen 、Miami
+(852) 6501 3956
Sat-Mon:10.00AM-5.00PM