不管 Recaptcha 是否提交联系表单

Contact form submitting regardless of Recaptcha

您好,我正在尝试将 Google Recaptcha 安装到我的联系表中。 Recaptcha 显示正常,但目前即使未尝试验证码,表单也会提交。

我已经在下面发布了代码,非常感谢您的帮助:)

<?php

/* =====================================================
 * change this to the email you want the form to send to
 * ===================================================== */
$email_to = ";
$email_subject = "Contact Form submitted";

if(isset($_POST['email']))
{

    function return_error($error)
    {
        echo $error;
        die();
    }

    // check for empty required fields
    if (!isset($_POST['name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['message'])

)
    {
        return_error('Please fill in all required fields.');
    }

    // form field values
    $name = $_POST['name']; // required
    $email = $_POST['email']; // required
    $contact_number = $_POST['contact_number']; // not required
    $message = $_POST['message']; // required
    $enquiry = $_POST['enquiry'];


    // form validation
    $error_message = "";

    // name
    $name_exp = "/^[a-z0-9 .\-]+$/i";
    if (!preg_match($name_exp,$name))
    {
        $this_error = 'Please enter a valid name.';
        $error_message .= ($error_message == "") ? $this_error : "<br/>".$this_error;
    }        

    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    if (!preg_match($email_exp,$email))
    {
        $this_error = 'Please enter a valid email address.';
        $error_message .= ($error_message == "") ? $this_error : "<br/>".$this_error;
    } 

    // if there are validation errors

    if(strlen($error_message) > 0)
    {
        return_error($error_message);
    }

    // prepare email message
    $email_message = "Form details below.\n\n";

    function clean_string($string)
    {
        $bad = array("content-type", "bcc:", "to:", "cc:", "href");
        return str_replace($bad, "", $string);
    }

    $email_message .= "Enquiry Type: ".clean_string($enquiry)."\n";
    $email_message .= "Name: ".clean_string($name)."\n";
    $email_message .= "Email: ".clean_string($email)."\n";
    $email_message .= "Contact number: ".clean_string($contact_number)."\n";
    $email_message .= "Message: ".clean_string($message)."\n";

    // create email headers
    $headers = 'From: '.$email."\r\n".
    'Reply-To: '.$email."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    //var_dump($email_to); var_dump($email_subject); var_dump($email_message); var_dump($headers);  
    if (mail($email_to, $email_subject, $email_message, $headers))
    {
        echo 'Form submitted successfully.';
    }
    else 
    {
        echo 'An error occured. Please try again later.';
        die();        
    }
}
else
{
    echo 'Please fill in all required fields.';
    die();
}
?>

<?php   
    if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
        $privatekey = "";

        //get verified response data
        $param = "https://www.google.com/recaptcha/api/siteverify?secret=".$privatekey."&response=".$_POST['g-recaptcha-response'];
        $verifyResponse = file_get_contents($param);
        $responseData = json_decode($verifyResponse);

        if($responseData->success){
            // success
            echo "success";

        }else{
            // failure
            echo "failure";
        }
    }else{
        // user didn't enter reCAPTCHA
        echo "The reCAPTCHA wasn't entered correctly. Go back and try it again.";
    }
?>

仔细查看代码,最外层的 if 块,即 if(isset($_POST['email'])){ ...if(isset($_POST['g-recaptcha-response'])... 将独立工作。您需要将 if(isset($_POST['email'])){ ... 块放在 if(isset($_POST['g-recaptcha-response'])... 块内,如下所示:

if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
    $privatekey = "YOUR_PRIVATE_KEY";

    //get verified response data
    $param = "https://www.google.com/recaptcha/api/siteverify?secret=".$privatekey."&response=".$_POST['g-recaptcha-response'];
    $verifyResponse = file_get_contents($param);
    $responseData = json_decode($verifyResponse);

    if($responseData->success){
        // success
        $email_to = '';
        $email_subject = "Contact Form submitted";

        if(isset($_POST['email'])){

            function return_error($error){
                echo $error;
                die();
            }

            // check for empty required fields
            if (!isset($_POST['name']) || !isset($_POST['email']) || !isset($_POST['message'])){
                return_error('Please fill in all required fields.');
            }

            // form field values
            $name = $_POST['name']; // required
            $email = $_POST['email']; // required
            $contact_number = $_POST['contact_number']; // not required
            $message = $_POST['message']; // required
            $enquiry = $_POST['enquiry'];


            // form validation
            $error_message = "";

            // name
            $name_exp = "/^[a-z0-9 .\-]+$/i";
            if (!preg_match($name_exp,$name)){
                $this_error = 'Please enter a valid name.';
                $error_message .= ($error_message == "") ? $this_error : "<br/>".$this_error;
            }        

            $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
            if (!preg_match($email_exp,$email)){
                $this_error = 'Please enter a valid email address.';
                $error_message .= ($error_message == "") ? $this_error : "<br/>".$this_error;
            } 

            // if there are validation errors

            if(strlen($error_message) > 0){
                return_error($error_message);
            }

            // prepare email message
            $email_message = "Form details below.\n\n";

            function clean_string($string){
                $bad = array("content-type", "bcc:", "to:", "cc:", "href");
                return str_replace($bad, "", $string);
            }

            $email_message .= "Enquiry Type: ".clean_string($enquiry)."\n";
            $email_message .= "Name: ".clean_string($name)."\n";
            $email_message .= "Email: ".clean_string($email)."\n";
            $email_message .= "Contact number: ".clean_string($contact_number)."\n";
            $email_message .= "Message: ".clean_string($message)."\n";

            // create email headers
            $headers = 'From: '.$email."\r\n".
            'Reply-To: '.$email."\r\n" .
            'X-Mailer: PHP/' . phpversion();
            //var_dump($email_to); var_dump($email_subject); var_dump($email_message); var_dump($headers);  
            if (mail($email_to, $email_subject, $email_message, $headers)){
                echo 'Form submitted successfully.';
            }else{
                echo 'An error occured. Please try again later.';
                die();        
            }
        }else{
            echo 'Please fill in all required fields.';
            die();
        }

    }else{
        // failure
        echo "reCAPTCHA failure";
    }
}else{
    // user didn't enter reCAPTCHA
    echo "The reCAPTCHA wasn't entered correctly. Go back and try it again.";
}