Skip to content

Instantly share code, notes, and snippets.

@stevesohcot
Created November 27, 2023 01:37
Show Gist options
  • Save stevesohcot/1ea85c822a332e573a485ecdf4e8ac00 to your computer and use it in GitHub Desktop.
Save stevesohcot/1ea85c822a332e573a485ecdf4e8ac00 to your computer and use it in GitHub Desktop.
Google recaptcha with PHP - sign up controller
<?php
if (array_key_exists('signUpAttempt', $_POST)) {
$captcha = isset($_POST['g-recaptcha-response']) ? $_POST['g-recaptcha-response'] : null;
// ultimately these could be constants in a "secrets" file
// aka stored as environment variables
#$captchaPublic = constant('CAPTCHA_PUBLIC');
#$captchaSecret = constant('CAPTCHA_SECRET');
$captchaPublic = "my-public-key-here";
$captchaSecret = "my-secret-key-here";
$res = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$captchaSecret ."&response=".$captcha));
if($res->success === true){
// ok! don't do anything
} else{
print "Error: Human check failed";
exit();
}
// continue sign up process as normal
print "You don't seem to be a robot";
exit();
#$email = $_POST['email'] ?? '';
#$password = $_POST['password'] ?? '';
// ...
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment