Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I was using the old ReCaptcha on my site but just upgrading to the new ReCaptcha i've 3-4 different tutorials but it keeps failing and giving the fail error.

HTML

<form method="POST" action="/feedback_sent/" enctype="multipart/form-data">
<table border="0" width="100%" cellpadding="4" cellspacing="4" height="67">
    <tr>
        <td width="30%" height="30">Name</td>
        <td width="70%" height="30">
            <p><input type="text" name="name" size="41"></p>
        </td>
    </tr>
    <tr>
        <td valign="top" width="30%" height="27">Feedback</td>
        <td width="70%" height="27">
        <textarea rows="8" name="feedback" cols="57"></textarea></td>
    </tr>   
    <tr>
        <td valign="top" width="30%" height="27"></td>
        <td width="70%" height="27">
        <div class="g-recaptcha" data-sitekey="My_Key"></div>
        </td>
    </tr>
    <tr>
        <td width="100%" colspan="2">
        <p align="center"><input type="submit" value="Submit" name="B1"></td>
    </tr>
</table>


</form>

PHP:

    $secret="---my key---";
    $response=$_POST["g-recaptcha-response"];
    $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
    $captcha_success=json_decode($verify);

    if ($captcha_success->success==false) {
        echo "<p>You are a bot! Go away!</p>";
    }
    else if ($captcha_success->success==true) {
            echo "<p>You are not not a bot!</p>";
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
336 views
Welcome To Ask or Share your Answers For Others

1 Answer

Step 1:

You will need to create a public and a private key here.

Step 2:

Include this library to process the information on the server.

The code:

<?php
  $public  = "yourkey";
  $private = "yourkey";
  $lang    = "en";

  if(isset($_POST['submit'])){
    if(array_key_exists('g-recaptcha-response', $_POST)){
      require 'path/to/recaptcha/autoload.php';
      $c = new ReCaptchaReCaptcha($private);

      $r = $c->verify(
        $_POST["g-recaptcha-response"],
        $_SERVER["REMOTE_ADDR"]
      );

      if($r->isSuccess()){
        echo 'success';
      } else {
        echo 'failed';
      }
    } else {
      die('client failure, enable js or update browser');
    }
  } else {
    die('form failure');
  }
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>ReCaptcha 2.0</title>
  </head>
  <body>
     <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
      <input for="email" name="email" type="email" required>
      <input for="password" name="password" type="password" required>
      <div class="g-recaptcha" data-sitekey="<?= $public ?>"></div>
      <input type="submit" name="submit" value="Submit">
    </form>
    <script src='https://www.google.com/recaptcha/api.js?hl=<?= $lang ?>'></script>
  </body>
</html>

This will say either success or failed depending on the input.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...