reCaptcha integration with php
Posted
by Neil Bradley
on Stack Overflow
See other posts from Stack Overflow
or by Neil Bradley
Published on 2010-05-13T08:38:11Z
Indexed on
2010/05/13
8:44 UTC
Read the original article
Hit count: 261
Hi there,
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 = "6LcbbQwAAAAAAPYy2EFx-8lFCws93Ip6Vi5itlpT";
$privatekey = "6LcbbQwAAAAAAPV_nOAEjwya5FP3wzL3oNfBi21C";
# 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; ?>
© Stack Overflow or respective owner