Created
November 27, 2023 01:37
-
-
Save stevesohcot/1ea85c822a332e573a485ecdf4e8ac00 to your computer and use it in GitHub Desktop.
Google recaptcha with PHP - sign up controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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