Интеграция Recaptcha с существующей формой проверки - PullRequest
0 голосов
/ 06 сентября 2011

Я не очень хорошо разбираюсь в 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 голосов
/ 06 сентября 2011

Следующий код должен поместить ошибку 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 голосов
/ 06 сентября 2011

Я не совсем уверен, что именно вы спрашиваете, но:

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 и подойдет для вашей проверки ошибок.

...