[PHP] reCAPTCHA v3 사용법


본문
reCAPTCHA 관리 콘솔
https://www.google.com/recaptcha/admin/create?hl=ko
1.HTML에 스크립트 추가
<script src="https://www.google.com/recaptcha/api.js?render=SITE_KEY"></script>
2.토큰 요청 및 서버로 전송
<script>
grecaptcha.ready(function() {
grecaptcha.execute('SITE_KEY', {action: 'homepage'}).then(function(token) {
// token을 서버에 전송
document.getElementById('recaptcha_token').value = token;
});
});
</script>
<form action="/submit" method="POST">
<input type="hidden" name="recaptcha_token" id="recaptcha_token">
<!-- 나머지 폼 입력들 -->
<button type="submit">제출</button>
</form>
3.서버에서 토큰 검증
$recaptcha_token = $_POST['recaptcha_token'];
$secret_key = 'YOUR_SECRET_KEY';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'secret' => $secret_key,
'response' => $recaptcha_token
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$response_data = json_decode($response, true);
if ($response_data["success"] && $response_data["score"] >= 0.5) {
// 정상적인 사용자
} else {
// 봇일 가능성 높음
}
댓글목록0
댓글 포인트 안내