php – ReCaptcha一直说我是机器人,不会成功吗?

前端之家收集整理的这篇文章主要介绍了php – ReCaptcha一直说我是机器人,不会成功吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的网站上使用旧的ReCaptcha,但只是升级到新的ReCaptcha我有3-4个不同的教程,但它一直失败并给出失败错误.

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>";
    }
步骤1:

您需要创建公钥和私钥here.

第2步:

包含this库以处理服务器上的信息.

代码

<?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 ReCaptcha\ReCaptcha($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>

这将说明成功或失败取决于输入.

猜你在找的PHP相关文章