Created
June 13, 2017 23:06
-
-
Save Yelakelly/ce47b596b81d3fc63a64e3052b0a9a08 to your computer and use it in GitHub Desktop.
Google captcha snippet
This file contains hidden or 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 | |
$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