Created
July 5, 2023 12:43
-
-
Save mpdroog/972834cb81b9f1f7d5adceb94f9d8a6c to your computer and use it in GitHub Desktop.
Google Recaptcha verify
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 | |
$ch = curl_init(); | |
if ($ch === false) { | |
user_error("curl_init fail"); | |
} | |
$opt = 1; | |
$opt &= curl_setopt($ch, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify"); | |
$opt &= curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$opt &= curl_setopt($ch, CURLOPT_TIMEOUT, 3); | |
$opt &= curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); | |
$opt &= curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([ | |
"secret" => "SUPERSECRETHERE", | |
"response" => $input->g_recaptcha_response, | |
"remoteip" => Env::ip() | |
])); | |
if ($opt !== 1) { | |
user_error("curl_setopt fail"); | |
} | |
$res = curl_exec($ch); | |
if ($res === false) { | |
user_error("curl_exec fail"); | |
} | |
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
if ($code !== 200) { | |
error_log("google-recaptcha: " . $res); | |
Shared::client_error("google recaptcha fail"); | |
exit(); | |
} | |
$j = json_decode($res, true); | |
if (! $j["success"]) { | |
Shared::client_error("google recaptcha invalid"); | |
exit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment