Skip to content

Instantly share code, notes, and snippets.

@NachoToast
Created April 27, 2021 06:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save NachoToast/f54bcf275a85b242a82d1a9f9e027321 to your computer and use it in GitHub Desktop.
Save NachoToast/f54bcf275a85b242a82d1a9f9e027321 to your computer and use it in GitHub Desktop.
Verify a recaptcha v3 token.
<?php
function verify_captcha($token) {
$threshold = 0.5; // Score must be > threshold to pass captcha.
// Default is 0.5, although the majority of users will get 0.9
$sites = ["localhost", "nachotoast.com", "ntgc.ddns.net"]; // Site names string, e.g. sub.domain.com:8080
$secret = "Put your client secret here.";
$url = "https://www.google.com/recaptcha/api/siteverify";
$data = array("secret" => $secret, "response" => $token);
$options = array(
"http" => array(
"header" => "Content-type: application/x-www-form-urlencoded\r\n",
"method" => "POST",
"content" => http_build_query($data)
)
);
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
$response_keys = json_decode($response, true);
// error checks
if (isset($response_keys["error-codes"])) {
if (in_array("timeout-or-duplicate", $response_keys["error-codes"])) return "expired";
return $response_keys["error-codes"];
}
// success check (theoretically not needed due to above error checks)
if ($response_keys["success"] !== true) return "invalid-token";
// score check
if ($response_keys["score"] < $threshold) return "failed";
// hostname check
if (!in_array($response_keys["hostname"], $sites)) return "wrong-site";
// action check
if ($response_keys["action"] !== "submit") return "wrong-action";
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment