Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

(❀╹◡╹)

[PHP] 구글 reCaptcah v3 본문

Programing/WEB

[PHP] 구글 reCaptcah v3

진 주 2020. 4. 2. 14:21

 

https://www.google.com/recaptcha/intro/v3.html

 

reCAPTCHA

reCAPTCHA is a free security service that protects your websites from spam and abuse.

www.google.com

사이트 접속 > 상단 'Admin Console' 클릭

새 사이트 만들기 > reCaptcha 유형 v3 선택 후 진행

 

 

코드

프론트단

- form 태그 안에 아래 input 추가

<input type="hidden" id="g-recaptcha" name="g-recaptcha">

- 스크립트 추가

<script src="https://www.google.com/recaptcha/api.js?render=사이트키"></script>
<script type="text/javascript">
grecaptcha.ready(function() {
    grecaptcha.execute('사이트키', {action: 'homepage'}).then(function(token) {
       document.getElementById('g-recaptcha').value = token;
    });
});
</script>

 

백단

$captcha = $_POST['g-recaptcha'];
$secretKey = '비밀키';
$ip = $_SERVER['REMOTE_ADDR']; // 옵션값

$data = array(
  'secret' => $secretKey,
  'response' => $captcha,
  'remoteip' => $ip
);

$url = 'https://www.google.com/recaptcha/api/siteverify';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);

$responseKeys = json_decode($response, true);

if (!$responseKeys['success']) {
	// 검증 실패 처리
}