Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

php - ReCaptcha keeps saying im a bot, doesn't ever succeed?

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...