(❀╹◡╹)
[PHP] 구글 reCaptcah v3 본문
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']) {
// 검증 실패 처리
}
'Programing > WEB' 카테고리의 다른 글
[JS] IE에서 JavaScript 요소 remove() 처리 (0) | 2020.06.10 |
---|---|
[Mssql] group_concat 함수처럼 조회하기 (0) | 2020.04.17 |
[Oracle] MySql Limit 와 같이 데이터 조회하기 (0) | 2020.03.26 |
[PHP] preg_match_all 에러 (0) | 2020.02.04 |
[JS + PHP] 이미지 업로드 시 자동 회전되는 문제 해결 (0) | 2020.01.20 |