【问题标题】:Integrating Recaptcha with existing form validation将 Recaptcha 与现有的表单验证集成
【发布时间】:2011-09-06 19:09:47
【问题描述】:

我不太擅长 PHP,所以在将 RECAPTCHA 与现有验证集成时遇到了麻烦。

$post = $_POST;

$samples = Array();
if(isset($post['business_samples'])) {
    foreach($post['business_samples'] as $sample) {
        $samples[] = $sample;
    }
} else {
    $tmp = explode("\n", $post['samples']);
    foreach($tmp as $sample) {
        $samples[] = $sample;
    }
}

$errors = Array();
if(empty($post['contact_name'])) {
    $errors[] = "You must provide a valid contact name.";
}
if(isset($post['business_samples'])) {
    if(empty($post['company_name'])) {
        $errors[] = "You must provide a valid company name.";
    }
}
if(empty($post['company_size'])) {
    $errors[] = "You must provide a valid company size.";
}
if(empty($post['address'])) {
    $errors[] = "You must provide a valid address.";
}
if(empty($post['city'])) {
    $errors[] = "You must provide a valid city.";
}
if(empty($post['state'])) {
    $errors[] = "You must provide a valid state.";
}
if(empty($post['zip'])) {
    $errors[] = "You must provide a valid zip.";
}

if(count($errors) != 0) {
    $json = '{"success":false, "errors": [';
    foreach($errors as $error) {
        $json .= '"'.$error.'",';
    }
    $json .= '""]}';
    echo $json;
    exit;
}

这应该在表单之上,在现有代码之上。

?>
<?php require_once('recaptchalib.php');
$privatekey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$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
}
?>

我们将不胜感激。

【问题讨论】:

    标签: php recaptcha


    【解决方案1】:

    我不太确定你在问什么,具体来说,但是:

    if(empty($post['zip'])) {     
        $errors[] = "You must provide a valid zip."; 
    }  
    
    if (!$resp->is_valid) { 
        $errors[]="The reCAPTCHA wasn't entered correctly. Go back and try it again." ."(reCAPTCHA said: " . $resp->error.")"); 
    }
    
    if(count($errors) != 0) {     
        $json = '{"success":false, "errors": [';     
        foreach($errors as $error) {         
            $json .= '"'.$error.'",';     
        }     
        $json .= '""]}';     
        echo $json;     
        exit; 
    }  
    

    我认为它可以用于检查 recaptcha 并适合您的错误检查。

    【讨论】:

      【解决方案2】:

      以下代码应将 reCAPTCHA 错误放入您的 $errors 数组中。

      require_once('recaptchalib.php');
      $privatekey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
      $resp = recaptcha_check_answer ($privatekey,
                              $_SERVER["REMOTE_ADDR"],
                              $_POST["recaptcha_challenge_field"],
                              $_POST["recaptcha_response_field"]);
      
      $errors = Array();
      if(empty($post['contact_name'])) {
          $errors[] = "You must provide a valid contact name.";
      }
      // ....snip....
      // reCAPTCHA error
      if (!$resp->is_valid) {
          $errors[] = "reCAPTCHA error: " . $resp->error;
      }
      

      【讨论】:

      • 它似乎将错误放在我想要的位置,但现在我只是收到错误“reCAPTCHA 错误:不正确的验证码溶胶”,无论答案是对还是错都无关紧要。我错过了什么吗?
      • 我认为您的表单可能有问题,请将您的表单代码发布到 pastie 或类似的地方。
      猜你喜欢
      • 1970-01-01
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      • 1970-01-01
      • 2011-07-06
      • 2016-08-13
      相关资源
      最近更新 更多