将Recaptcha与现有表单验证集成

时间:2011-09-06 19:09:47

标签: php recaptcha

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

帮助将非常感激。

2 个答案:

答案 0 :(得分:0)

我不是很确定你在问什么,特别是,但是:

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并适合您的错误检查。

答案 1 :(得分:0)

以下代码应将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;
}