Skip to content

Instantly share code, notes, and snippets.

@amityweb
Last active June 16, 2020 09:59
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 amityweb/7175c5797d96005dce11e12677364e4b to your computer and use it in GitHub Desktop.
Save amityweb/7175c5797d96005dce11e12677364e4b to your computer and use it in GitHub Desktop.
Google ReCaptcha v2 PHP Code
// Add in to HTML where you want the box to be
<script src='https://www.google.com/recaptcha/api.js' async defer></script>
<div class="captcha_wrapper">
<div class="g-recaptcha" data-sitekey="YOUR_PUBLIC_KEY"></div>
</div>
// Add into PHP validation to check the submitted captcha response is a success or fail
/* Google ReCaptcha Verification */
$url = 'https://www.google.com/recaptcha/api/siteverify';
$privatekey = "YOUR_PRIVATE_KEY";
$response = file_get_contents($url."?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
$data = json_decode($response);
if (isset($data->success) AND $data->success==true)
{
// Do Success Stuff!
}
else
{
// Do Fail Stuff!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment