ReCaptcha始终返回false,没有错误

时间:2012-01-14 07:55:05

标签: php recaptcha

我已经安装了ReCaptcha,配置了我的公钥和私钥,一切都很好,直到我输入任何答案,无论它是好还是坏,它甚至没有响应发生的错误。

这是我的代码:

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;
}

2 个答案:

答案 0 :(得分:5)

经过几天的搜索,我找到了答案......

如果有人遇到类似的问题,这是解决方案:

rechaptchalib.php更改:

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'));

致:

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");

这可能是由于过时的PHP库引起的,所以最好还是下载最新的库:

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

谢谢大家。

答案 1 :(得分:0)

您需要在表单文件中使用公钥。配置文件中的私钥:

<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>

这是表单请求的文件:

<?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
  }
  ?>

如需进一步帮助,您可以访问:http://code.google.com/apis/recaptcha/docs/php.html

相关问题