New Google ReCaptcha not posting/receiving 'g-

2019-01-28 00:50发布

I am trying to implement Googles new 'NoCaptcha' on my site. So far I have the widget appearing fine, but it does not verify on my PHP page.

My code is set up as such:

In <head>

<script src='https://www.google.com/recaptcha/api.js'></script>

Client Side:

<form id="contactform" action="bookingverify.php" method="POST">
<input type="text" name="name" size="41">
<!--OTHER FORM INPUTS-->
<div class="g-recaptcha" data-sitekey="mypublickey"></div>
</form>

Server Side (bookingverify.php)

  $captcha;
  if(isset($_POST['g-recaptcha-response'])){
       $captcha=$_POST['g-recaptcha-response'];
     }
  if(!$captcha){
      echo '<h2>Please check the the captcha form.</h2>';
       exit;
   }

  $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=myprivatekey&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);

  if($response.success==false){
          echo '<h2>You are spammer</h2>';
  }
  else{
     //SEND MAIL
  }

I've tried echoing $_POST['g-recaptcha-response'] but it appears empty. It's like that variable is not being posted to the php.

Does anyone know what I am doing wrong here?

3条回答
The star\"
2楼-- · 2019-01-28 01:02

Your code ran fine on my test server with my own private / public key. It seems trivial, but the only thing I had to add - do you have a submit button in your other form inputs? This is what actually posts the data to your PHP script.

<input type="submit">
</form>

Otherwise, add var_dump($_POST['g-recaptcha-response']); to your bookingverify.php and see what it outputs.

查看更多
叼着烟拽天下
3楼-- · 2019-01-28 01:11

OK I have no idea why but I deleted the exit; in the 2nd IF statement and it worked. Weird.

查看更多
倾城 Initia
4楼-- · 2019-01-28 01:24

i had a similar issue. Apparently if you dont click the checkbox the $_POST['g-recaptcha-response'] comes in empty. so just make sure you click it when testing

查看更多
登录 后发表回答