구글 리캡차 주소
https://www.google.com/recaptcha
1. 간단하게 리캡차 넣기
head 태그 사이에 다음을 넣는다.
<script src=”https://www.google.com/recaptcha/api.js” async defer></script>
그리고 사이트키와 함께 다음을 추가한다.
<div class=”g-recaptcha” data-sitekey=”사이트키”></div>
2.리캡차 안하면 경고창 뛰우기
체크 안하면 checkrecaptachSubmit() 이라고 이름지은 함수가 뜨게 다음을
폼메일 액션자리에 넣는다.
<form method=”post” action=”사용하는폼메일.php” onsubmit=”return checkrecaptachSubmit();”>
그리고 checkrecaptachSubmit() 함수를 head 태그 사이에 넣는다.
<script type="text/javascript"> function checkrecaptachSubmit() { if (grecaptcha.getResponse() == ""){ alert("Please check the reCaptcha"); return false;} else { return true;}} </script>
3.서버 쪽 Validation
<?php
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<h2리캡차를 확인하세요</h2>';
exit;
}
$secretKey = "시크릿키";
$ip = $_SERVER['REMOTE_ADDR'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response,true);
if(intval($responseKeys["success"]) !== 1) {
echo '<h2>너 스패머지? 나가라</h2>';
} else {
echo '<h2>고맙습니다.</h2>';
}
?>