ReCaptcha always returns false, with no error

2019-05-11 15:19发布

I've installed the ReCaptcha, configured my public and private key, and everything is fine until I type any answer on it, no matter if it's good or wrong, it doesn't even respond with the error there was occurred.

Here is my code:

require_once('recaptchalib.php');
$resp = recaptcha_check_answer ($config->privkey,   $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
    echo $resp->error;
}

标签: php recaptcha
2条回答
贼婆χ
2楼-- · 2019-05-11 15:46

I've found the answer after days of searching...

Here is the solution if someone is having a similar problem:

In your rechaptchalib.php change:

define("RECAPTCHA_API_SERVER", "http://api.recaptcha.net");
define("RECAPTCHA_API_SECURE_SERVER", "https://api-secure.recaptcha.net");
define("RECAPTCHA_VERIFY_SERVER", gethostbyname('api-verify.recaptcha.net'));

To the:

define("RECAPTCHA_API_SERVER", "http://www.google.com/recaptcha/api");
define("RECAPTCHA_API_SECURE_SERVER", "https://www.google.com/recaptcha/api");
define("RECAPTCHA_VERIFY_SERVER", "www.google.com");

It was probably caused by the outdated PHP library, so it'll be better for you to download the latest library also:

http://code.google.com/p/recaptcha/downloads/list?q=label:phplib-Latest

Thanks everyone.

查看更多
聊天终结者
3楼-- · 2019-05-11 15:54

You need to have public key in your form file & private key in config file:

<html>
<body> <!-- the body tag is required or the CAPTCHA may not show on some browsers -->
  <!-- your HTML content -->

  <form method="post" action="verify.php">
    <?php
      require_once('recaptchalib.php');
      $publickey = "your_public_key"; // you got this from the signup page
      echo recaptcha_get_html($publickey);
    ?>
    <input type="submit" />
  </form>

  <!-- more of your HTML content -->
</body>
</html>

This is your file where form request goes:

<?php
  require_once('recaptchalib.php');
  $privatekey = "your_private_key";
  $resp = recaptcha_check_answer ($privatekey,
                            $_SERVER["REMOTE_ADDR"],
                            $_POST["recaptcha_challenge_field"],
                            $_POST["recaptcha_response_field"]);

  if (!$resp->is_valid) {
    // What happens when the CAPTCHA was entered incorrectly
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
     "(reCAPTCHA said: " . $resp->error . ")");
  } else {
    // Your code here to handle a successful verification
  }
  ?>

For further assistance you can go to: http://code.google.com/apis/recaptcha/docs/php.html

查看更多
登录 后发表回答