reCaptcha无法验证

时间:2014-12-08 23:24:43

标签: php recaptcha

我的reCaptcha实现不起作用。我研究了Google的文档,但它不起作用。

我实现的插件如下:

<form role="form" name="formular" method="post" onsubmit="return chkFormular()">
        <div class="form-group" id="name">
            <label class="control-label" for="name">Name</label>
            <input type="text" class="form-control" name="name" placeholder="Name">
        </div>
        <div class="g-recaptcha" data-sitekey="my_public_key"></div>
        <button type="submit" class="btn btn-default">Abschicken</button>
 </form>

验证码工作正常。但现在我想这样验证它:

require_once('../php/recaptchalib.php');
$privatekey = "my_private_key";
$publickey = "my_public_key";

# the response from reCAPTCHA
$resp = null;

# was there a reCAPTCHA response?
if( $_POST["recaptcha_response_field"] ) 
{
   $resp = recaptcha_check_answer (
            $privatekey, $_SERVER["REMOTE_ADDR"], 
            $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
      if( $resp->is_valid ) 
      {
        //send_mail
        mail();
      } 
      else {
         if( $resp->error == 'incorrect-captcha-sol') {
            // do something
         }
   }
}

但它不会调用mail()方法。

2 个答案:

答案 0 :(得分:0)

PHP的mail函数有三个必需参数。因为这个原因,它很可能会失败。您还应该考虑使用newly released reCAPTCHA version

答案 1 :(得分:-1)

我可以自己解决问题。

我在我的表单中插入了像这样的验证码:

<div class="g-recaptcha " data-sitekey="6Ldj_v4SAAAAAF8dEtmmJ0kv8kkLLEJuQfcdCOMZ"></div>

然后我添加这个php脚本:

if (isset($_POST["g-recaptcha-response"]) && !(empty($_POST["g-recaptcha-response"]))) {
    //send mail here
}

诀窍是,如果验证码没有解决,帖子中的“g-recaptcha-response”是空的。否则会出现一个不可读的字符串。