2011-09-06 88 views
0

我对PHP不太擅长,所以我在将RECAPTCHA与现有验证集成时遇到了问题。将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 
} 
?> 

帮助将非常感激。

回答

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

我想将工作检查验证码,并配合您的错误检查。

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

它似乎把错误放在我想要的地方,但现在它只是得到错误“reCAPTCHA错误:不正确的captcha-sol”并不重要,如果答案是对的或错误的。我错过了什么? – john

+0

我认为这可能与您的表单有关,请在[pastie](http://pastie.org)上发布您的表单代码或类似内容。 – Nexerus