I switched to a one page self-submitting form with reCaptcha, now it works ok, but it won't catch an error if you submit an empty captcha. The old two-page one did.
Should I do it with two pages? I've been working on this all day. Here's my code: notice that I've tried to flag the empty captcha string with some code of my own (if $answer == "" ) but it doesn't work. Why? Suggestions?
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>contact form</title>
</head>
<body>
<form action="" method="post">
<?php
# Get the reCAPTCHA library
require_once('recaptchalib.php');
# These are /not/ real keys - you must replace them with your *own* keys
# obtained from http://recaptcha.net/api/getkey
#define('PUBLIC_KEY', '6LeLTQEAAAAAAFwsoz6WomDxk0Mrs9MEaNOPgOc6');
#define('PRIVATE_KEY', ....);
$privatekey = ......;
$publickey = '6LeLTQEAAAAAAFwsoz6WomDxk0Mrs9MEaNOPgOc6';
# Did the user fail the captcha test?
#$error = "";
# This is where we process the user's response. We don't
# do this when the form is initially displayed - only
# when the user submits it.
if ($_POST["recaptcha_response_field"]) {
$response = recaptcha_check_answer(
$privatekey, $_SERVER['REMOTE_ADDR'],
$_POST['recaptcha_challenge_field'],
$_POST['recaptcha_response_field']
);
$answer = $_POST['recaptcha_response_field'];
if($answer == "") {
echo 'please fill in reCaptcha';
# The user passed the reCAPTCHA test: form submission should continue
# Your other form validation logic should go here.
# For example
# ... validate user input ...
# ... store form data in a database ...
# ... redirect to 'thank you' page
}
else if
($response->is_valid) {
echo "hello world";
echo "<br />";
$username=$_POST['username'];
$email=$_POST['email'];
echo "username is: ";
echo $username;
echo "<br />";
echo "email is: ";
echo $email;
# The user passed the reCAPTCHA test: form submission should continue
# Your other form validation logic should go here.
# For example
# ... validate user input ...
# ... store form data in a database ...
# ... redirect to 'thank you' page
}
else {
# The user failed the reCAPTCHA test so we need
# to fill in the error message and re-try the
# form submission
#echo "Please reenter reCaptcha words";
$error = $response->error;
die ("The reCAPTCHA was entered incorrectly. Please enter the new reCaptcha words");
}
}
#I decided to place the reCAPTCHA challenge in the form below.....
# Display the reCAPTCHA challenge. The first time
# through $error will be null.
?>
<br />
<!-- example form fields - your own fields go here -->
Username: <input type="text" name="username" value="" /><br />
Email address: <input type="text" name="email" value="" /><br />
<?php
#require_once('recaptchalib.php');
#$publickey = "6LeLTQEAAAAAAFwsoz6WomDxk0Mrs9MEaNOPgOc6"; // you got this from the signup page
echo recaptcha_get_html($publickey, $error);
?>
<input type="submit" value="submit" />
</form>
</body>
</html>
|