Skip to content

Instantly share code, notes, and snippets.

@Yelakelly
Created June 13, 2017 23:06
Show Gist options
  • Save Yelakelly/ce47b596b81d3fc63a64e3052b0a9a08 to your computer and use it in GitHub Desktop.
Save Yelakelly/ce47b596b81d3fc63a64e3052b0a9a08 to your computer and use it in GitHub Desktop.
Google captcha snippet
<?php
$captcha_res = $_POST['g-recaptcha-response'];
$secret_code = "SECRET KEY";
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.google.com/recaptcha/api/siteverify");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"secret=" . $secret_code . "&response=". $captcha_res ."&remoteip=". $ip);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
$res = json_decode($server_output);
if($res->success){
// Do something
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment