Skip to content

Instantly share code, notes, and snippets.

@janus57
Created May 20, 2018 22:36
Show Gist options
  • Save janus57/13db173d2b15e7e5d009853aeb56b1b1 to your computer and use it in GitHub Desktop.
Save janus57/13db173d2b15e7e5d009853aeb56b1b1 to your computer and use it in GitHub Desktop.
Test ReCaptcha V2 - all in one
<?php
htmlentities(extract($_POST));
if ( $submit && isset($comment) ) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
'secret' => $private_key,
'response' => $_POST['g-recaptcha-response'],
'remoteip' => $_SERVER['REMOTE_ADDR']
]);
$resp = json_decode(curl_exec($ch));
curl_close($ch);
if ($resp->success) {
$status='Captcha valide !!</h3>';
} else {
$status='BOT DETECTED !!';
}
}
if ( !isset($public_key) && !isset($private_key) ) {
echo '<!doctype html>
<html lang="fr">
<head>
<title>Google recapcha test - API KEY</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form id="ipi_key" method="post">
<input type="text" name= "public_key" placeholder="Type your public_key" size="50"><br><br>
<input type="text" name= "private_key" placeholder="Type your private_key" size="50"><br><br>
<input type="submit" name="submit" value="Post API KEY"><br><br>
</form>
</body>
</html>';
}
elseif ( isset($public_key) && isset($private_key) ) {
$status = (isset($status)) ? $status : '';
echo '<!doctype html>
<html lang="fr">
<title>Google recapcha test - Submit and result</title>
<script src=\'https://www.google.com/recaptcha/api.js\'></script>
</head>
<body>
<h1>Google reCAPTHA test</h1>
<form id="comment_form" method="post">
<input type="email" name="email" placeholder="Type your email" size="40"><br><br>
<textarea name="comment" rows="8" cols="39"></textarea><br><br>
<input type="submit" name="submit" value="Post comment"><br><br>
<input type="hidden" name= "public_key" value="'.$public_key.'">
<input type="hidden" name= "private_key" value="'.$private_key.'">
<div class="g-recaptcha" data-sitekey="'.$public_key.'"></div>
</form>
<h2>API KEY</h2>
<pre>
Public API KEY used : '.$public_key.'
Private API KEY used : '.$private_key.'
</pre>
<h2>Result :</h2>
<h3>'. $status .'</h3>
</body>
</html>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment