15 11월

Google reCAPTCHA PHP (CURL/Snoopy) Sample

logo_recaptcha_color_24dp Google reCAPTCHA

입력폼

<form onsubmit="return check_form()">
    <input type="hidden" id="recaptcha_response" name="recaptcha_response" value="" />
    <div id="recaptcha1"></div>
    <button type="submit">확인</button>
</form>
<script type="text/javascript">
var recaptchaWidger1;
var onloadCallback = function() {
recaptchaWidger1 = grecaptcha.render('recaptcha1', {
'sitekey' : '{Site key}'
});
};
</script>
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" async defer></script>

<script>
function check_form() {
    var recaptch_response = grecaptcha.getResponse(recaptchaWidger1);
    if (!recaptch_response) {
        alert("자동가입방지 문자를 확인해 주세요");
        return false;
    }

    document.getElementById("recaptcha_response").value = recaptch_response;

    return true;
}
</script>

폼데이터 처리(1) – Snoopy.lib.php

include_once 'Snoopy.class.php';
$snoopy = new Snoopy;
$data = array(
    "secret" => "Secret key",
    "response" => $_POST['recaptcha_response']
);
$snoopy->submit('https://www.google.com/recaptcha/api/siteverify', $data);
$response = json_decode($snoopy->results);
if (!$response->success) {
    exit("fail message");
}

폼데이터 처리(2) – CURL

$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, "secret={Secret key}&response=".$_POST['recaptcha_response']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
curl_close ($ch);
$response = json_decode($output);
if (!$response->success) {
    exit("fail message");
}

답글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다.