Server : LiteSpeed System : Linux us-phx-web1202.main-hosting.eu 4.18.0-553.84.1.lve.el8.x86_64 #1 SMP Tue Nov 25 18:33:03 UTC 2025 x86_64 User : u615232177 ( 615232177) PHP Version : 8.1.33 Disable Function : NONE Directory : /home/u615232177/domains/imperialcafebistro.com/public_html/ |
<?php
// captcha.php
// Your reCAPTCHA keys and project info
$project_id = 'my-project-1014-1682620065870';
$site_key = '6LeTl-QrAAAAAN5xWX6VQwcqfh9UUhoADuSSr0e0';
$api_key = '6LeTl-QrAAAAAEZkBM-uM8tdsMnZ11NhjQ-YKZuj';
// Get the token from POST
$token = $_POST['g-recaptcha-response'] ?? '';
if (empty($token)) {
throw new \Exception('Captcha verification failed: no token received.');
}
// Send the token to Google for verification
$url = "https://recaptchaenterprise.googleapis.com/v1/projects/$project_id/assessments?key=$api_key";
$data = [
'event' => [
'token' => $token,
'siteKey' => $site_key,
'expectedAction' => 'submit',
],
];
$options = [
'http' => [
'header' => "Content-type: application/json\r\n",
'method' => 'POST',
'content' => json_encode($data),
'timeout' => 10,
],
];
$context = stream_context_create($options);
$response = @file_get_contents($url, false, $context);
if ($response === false) {
throw new \Exception('Captcha verification request failed.');
}
$result = json_decode($response, true);
// Validate the response
if (!isset($result['tokenProperties']['valid']) || !$result['tokenProperties']['valid']) {
$reason = $result['tokenProperties']['invalidReason'] ?? 'unknown';
throw new \Exception("Captcha token invalid: $reason");
}
if ($result['tokenProperties']['action'] !== 'submit') {
throw new \Exception('Captcha action mismatch.');
}
$score = $result['riskAnalysis']['score'] ?? 0.0;
// Fail if the score is too low
if ($score < 0.8) {
throw new \Exception('Please prove you are not a robot.');
}
// If we reach here, captcha passed
return true;
?>