md5hash16

Validate capcha

Sep 6th, 2020
142
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //validate
  2. $receivedRecaptcha = $_POST['recaptchaRes'];
  3. $google_secret =  "Yoursecretgooglepapikey";
  4. $verifiedRecaptchaUrl = 'https://www.google.com/recaptcha/api/siteverify?secret='.$google_secret.'&response='.$receivedRecaptcha;
  5. $handle = curl_init($verifiedRecaptchaUrl);
  6. curl_setopt($handle,  CURLOPT_RETURNTRANSFER, TRUE);
  7. curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false); // not safe but works
  8. //curl_setopt($handle, CURLOPT_CAINFO, "./my_cert.pem"); // safe
  9. $response = curl_exec($handle);
  10. $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
  11. curl_close($handle);
  12. if ($httpCode >= 200 && $httpCode < 300) {
  13.   if (strlen($response) > 0) {
  14.         $responseobj = json_decode($response);
  15.         if(!$responseobj->success) {
  16.             echo "reCAPTCHA is not valid. Please try again!";
  17.             }
  18.         else {
  19.             echo "reCAPTCHA is valid.";
  20.         }
  21.     }
  22. } else {
  23.   echo "curl failed. http code is ".$httpCode;
  24. }
RAW Paste Data Copied