Skip to content

Instantly share code, notes, and snippets.

@chienowa
Last active December 13, 2018 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chienowa/5e159e11b2b42a4101ba830fd5f869af to your computer and use it in GitHub Desktop.
Save chienowa/5e159e11b2b42a4101ba830fd5f869af to your computer and use it in GitHub Desktop.
reCAPTCHAver3 test
<html>
<head>
<script src='https://www.google.com/recaptcha/api.js?render={SITE_KEY}'></script>
</head>
<body>
<p> recaptcha form test</p>
<form id="form" method="post" action="send.php">
<input name="test01" type="text"><br>
<input name="test02" type="text"><br>
<input name="test02" type="text">
<input id="token" name="g-recaptcha-token" type="hidden" value="">
<input id="send" type="button" value="test recaptcha3">
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(function(){
$("#send").on("click",function(){
grecaptcha.ready(function() {
grecaptcha.execute('{SITE_KEY}', {action: 'homepage'}).then(function(token) {
if(token.length > 0) {
$("#token").val(token);
$("#form").submit();
} else {
return false;
}
});
}); //end of grecaptcha
}); //end of submit
});
</script>
</body>
</html>
<?php
define('CAPTCHA_SECRET',"{SECRET_KEY}");s
$post_data = http_build_query(
array(
'secret' => CAPTCHA_SECRET,
'response' => $_POST['g-recaptcha-token'],
'remoteip' => $_SERVER['REMOTE_ADDR']
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $post_data
)
);
$context = stream_context_create($opts);
$response = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context);
$result = json_decode($response);
echo "<pre>";
var_dump($result);
echo "</pre>";
if (!$result->success) {
throw new Exception('Verification failed!', 1);
} else {
// EXAMPLE
if($result->score > 0.7) echo "You're probably a human being!";
else if($result->score > 0.3) echo "You're may a human being!";
else echo "You must be a BOT!";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment