0

I have been looking for what I might be doing wrong and for solutions all over but none seem to rectify my problem. I have a form with a recaptcha, it is pretty simple. But my recaptcha validation fails every time and echo's "Not successful" when it should be successful. What am I doing wrong? Here is my code.

<?php require_once('src/autoload.php'); require_once('src/ReCaptcha/ReCaptcha.php'); require_once('src/ReCaptcha/RequestMethod.php'); require_once('src/ReCaptcha/RequestParameters.php'); require_once('src/ReCaptcha/Response.php'); require_once('src/ReCaptcha/RequestMethod/Post.php'); require_once('src/ReCaptcha/RequestMethod/Socket.php'); require_once('src/ReCaptcha/RequestMethod/SocketPost.php'); require_once('src/ReCaptcha/RequestMethod/Curl.php'); require_once('src/ReCaptcha/RequestMethod/CurlPost.php'); $gRecaptchaResponse = $_POST['g-recaptcha-response']; $remoteIp = $_SERVER['REMOTE_ADDR']; $SITEKEY = 'XXXX'; $secret = 'XXXX'; $recaptcha = new \ReCaptcha\ReCaptcha($secret); $resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp); if ($resp->isSuccess()) { echo "success"; } else { $errors = $resp->getErrorCodes(); echo "not success"; echo $errors; } ?> 
4
  • Have you tried without sending the user's IP. I have had issues before where it didn't work for some reason when doing this and after all, it's only optional. Commented Jan 4, 2017 at 10:34
  • @ThomasSmyth Doesnt work:( Commented Jan 4, 2017 at 10:37
  • How are you calling this file? Is it when you submit a form or is it an AJAX call from a button on the same page as the form? Commented Jan 4, 2017 at 10:39
  • Im calling this file when submitting a form Commented Jan 4, 2017 at 10:58

1 Answer 1

1
//reCaptcha $StrUrl = "https://www.google.com/recaptcha/api/siteverify"; $StrSecretKey = "XXXXXX"; $data = array('secret' => $StrSecretKey, 'response' => $_POST['Response']); $ch = curl_init($StrUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); $Response = curl_exec($ch); curl_close($ch); $Robot = json_decode($Response); $data = 0; if(isset($Robot->success) AND $Robot->success==true){ //CODE } 

Try this. It was originally designed for an AJAX call, however if you change the $_POST['Response'] to the name of the response variable then it should work, hopefully.

Sign up to request clarification or add additional context in comments.

1 Comment

No problem. Pleasure to be of assistance.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.