0

我正在建立一个也使用 reCaptcha 的联系我们页面,但我遇到了一些问题。我填写了联系表格中的所有字段和正确的 reCaptcha 单词,但表格没有提交。我假设这与验证有关,但想知道是否有人能够发现我哪里出错了?

我页面顶部的 PHP 代码如下所示;

<?php include('includes/session.php');

$err = '';
$success = '';
if(isset($_POST["docontact"]) && $_POST["docontact"] == "yes") {
    //get form details
    $form = new stdClass();
    $form->name = sanitizeOne($_POST["name"], "str");
    $form->email = sanitizeOne($_POST["email"], "str");
    $form->phone = sanitizeOne($_POST["phone"], "str");
    $form->mysevenprog = sanitizeOne($_POST["mysevenprog"], "str");
    $form->enquiry = sanitizeOne($_POST["enquiry"], "str");
    $form->howfindsite = sanitizeOne($_POST["howfindsite"], "str");

    //Check for errors (required: name, email, enquiry)
    if($form->name == "") {
        $err .= '<p class="warning">Please enter your name!</p>';
    }
    if($form->email == "") {
        $err .= '<p class="warning">Please enter your email address!</p>';
    }
    if($form->enquiry == "") {
        $err .= '<p class="warning">Please supply an enquiry message!</p>';
    }

    //Send Email
    if($err == "") {
        $mailer = new BlueMailer();
        $mailer->AddAddress(Configuration::getVar("developer_email"), Configuration::getVar("admin_email_name"));
        include('templates/email/contact-us-admin.php');
        if(!$mailer->Send()) {
            $err .= "<p>There was an error sending submitting your request!, Please try again later.";
        } else {
            $success = 'thanks';
        }

    }

} else {
    //Initialise empty variables
    $form = new stdClass();
    $form->name = "";
    $form->email = "";
    $form->phone = "";
    $form->mysevenprog = "";
    $form->enquiry = "";
    $form->howfindsite = "";
}
?>

然后在我的页面正文中,我的表格如下;

                <?php if($err != "") : ?>
                    <div class="error">
                        <?php echo $err; ?>
                    </div>
                <?php endif; ?>
                <?php if($success == 'thanks') : ?>
                <h3>Thank you for your enquiry</h3>
                <p>Your enquiry has been successfully sent. Someone will contact you shortly.</p>
                <?php else: ?>
                <h3>If you are looking to advertise with us, have some feedback about some of our programming or want to say 'Hi' please use the fields below</h3>

                <form name="contactus" id="contactus" method="post" action="<?php echo $_SERVER['SCRIPT_NAME'] ?>">
                    <ul>
                        <li><label for="name">Your name: *</label> <input name="name" id="name" class="textbox" style="width: 75%;" type="text" value="<?php echo $form->name ?>" /></li>
                        <li><label for="email">Email address: *</label> <input name="email" id="email" class="textbox" style="width: 75%;" type="text" value="<?php echo $form->email ?>" /></li>
                        <li><label for="phone">Telephone:</label>   <input name="phone" id="phone" class="textbox" style="width: 75%;" type="text" value="<?php echo $form->phone ?>" /></li>
                        <li><label for="mysevenprog">My Seven programme</label> <input name="mysevenprog" class="textbox" style="width: 75%;" type="text" value="<?php echo $form->mysevenprog ?>" /></li>  
                        <li><label for="enquiry">Enquiry/Message: *</label> <textarea name="enquiry" class="textarea" rows="5" cols="30" style="width: 75%;" id="enquiry"><?php echo $form->enquiry ?></textarea></li>
                        <li><label for="howfindsite">How did you find out about our site?</label> <input name="howfindsite" id="howfindsite" class="textbox" style="width: 75%;" type="text" value="<?php echo $form->howfindsite ?>" /></li>
                        <li>

                        <?php

                        require_once('recaptchalib.php');

                        // Get a key from http://recaptcha.net/api/getkey
                        $publickey = "";
                        $privatekey = "";

                        # the response from reCAPTCHA
                        $resp = null;
                        # the error code from reCAPTCHA, if any
                        $error = null;

                        # was there a reCAPTCHA response?
                        if ($_POST["recaptcha_response_field"]) {
                                $resp = recaptcha_check_answer ($privatekey,
                                                                $_SERVER["REMOTE_ADDR"],
                                                                $_POST["recaptcha_challenge_field"],
                                                                $_POST["recaptcha_response_field"]);

                                if ($resp->is_valid) {
                                        echo "You got it!";
                                } else {
                                        # set the error code so that we can display it
                                        $error = $resp->error;
                                }
                        }
                        echo recaptcha_get_html($publickey, $error);
                        ?>

                        </li>
                        <li><input type="submit" value="Submit Form" class="button" /></li>
                    </ul>
                    <input type="hidden" name="docontact" value="yes" />
                </form>
                <?php endif; ?>

表单在浏览器中呈现为这样;

        <form name="contactus" id="contactus" method="post" action="/contact-us2.php"> 
            <ul> 
                <li><label for="name">Your name: *</label> <input name="name" id="name" class="textbox" style="width: 75%;" type="text" value="" /></li> 
                <li><label for="email">Email address: *</label> <input name="email" id="email" class="textbox" style="width: 75%;" type="text" value="" /></li> 
                <li><label for="phone">Telephone:</label>   <input name="phone" id="phone" class="textbox" style="width: 75%;" type="text" value="" /></li> 
                <li><label for="mysevenprog">My Seven programme</label> <input name="mysevenprog" class="textbox" style="width: 75%;" type="text" value="" /></li>  
                <li><label for="enquiry">Enquiry/Message: *</label> <textarea name="enquiry" class="textarea" rows="5" cols="30" style="width: 75%;" id="enquiry"></textarea></li> 
                <li><label for="howfindsite">How did you find out about our site?</label> <input name="howfindsite" id="howfindsite" class="textbox" style="width: 75%;" type="text" value="" /></li> 
                <li> 

                <script type="text/javascript" src="http://api.recaptcha.net/challenge?k=XXXXXXXXXXXXXXXXXXX"></script> 

<noscript> 
    <iframe src="http://api.recaptcha.net/noscript?k=XXXXXXXXXXXXXXXXXXXX" height="300" width="500" frameborder="0"></iframe><br/> 
    <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea> 
    <input type="hidden" name="recaptcha_response_field" value="manual_challenge"/> 
</noscript> 
                </li> 
                <li><input type="submit" value="Submit Form" class="button" /></li> 
            </ul> 
            <input type="hidden" name="docontact" value="yes" /> 
        </form>
4

2 回答 2

1

Just use Zend_Service_ReCaptcha. You'll integrate this service just with few lines:

//Creating instance
$recaptcha = new Zend_Service_ReCaptcha($pubKey, $privKey);

//Display output
echo $recaptcha->getHTML();

//Handling input
$result = $recaptcha->verify(
    $_POST['recaptcha_challenge_field'],
    $_POST['recaptcha_response_field']
);

//And finally validate captcha
if ($result->isValid()) {
    //Cool!
}
于 2010-05-13T09:17:21.703 回答
0

查看使用 reCAPTCHA 阻止 PHP 中的垃圾邮件

于 2010-05-13T08:40:16.613 回答