reCaptcha 与 php 集成

发布于 2024-09-01 09:05:11 字数 8135 浏览 1 评论 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>

I'm building a contact us page that also uses a reCaptcha, but im having a few issues with it. I fill in all of the fields in the contact form and the correct reCaptcha words, but the form does not submit. I'm assuming this is something to do with the validation, but wondered if someone might be able to spot where i'm going wrong?

The PHP code at the top of my page looks like this;

<?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 = "";
}
?>

And then in the body of my page I have the form as follows;

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

The form gets rendered like this in the browser;

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一影成城 2024-09-08 09:05:11

只需使用Zend_Service_ReCaptcha。您只需几行代码即可集成此服务:

//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!
}

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!
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文