reCAPTCHA v3 사용법 > 웹프로그램

본문 바로가기

웹프로그램

[PHP] reCAPTCHA v3 사용법

profile_image
최고관리자
2025-07-16 09:38 18 0

본문

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

등록된 댓글이 없습니다.

댓글쓰기

적용하기
자동등록방지 숫자를 순서대로 입력하세요.
게시판 전체검색
상담신청