Reply
reCaptcha won't catch its own empty field
Old 08-07-2008, 03:09 PM reCaptcha won't catch its own empty field
matt w's Avatar
Skilled Talker

Posts: 80
Location: kalamazoo
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>
matt w is offline
Reply With Quote
View Public Profile Visit matt w's homepage!
 
When You Register, These Ads Go Away!
Old 08-07-2008, 03:55 PM Re: reCaptcha won't catch its own empty field
VirtuosiMedia's Avatar
Usability > SEO

Posts: 957
Try:
PHP Code:
 if (empty(trim($_POST['yourFieldHere']))){ 
__________________
Want new web resources every day? - Follow me on Twitter
RSS Feed Tutorial
VirtuosiMedia is offline
Reply With Quote
View Public Profile Visit VirtuosiMedia's homepage!
 
Old 08-08-2008, 11:15 AM Re: reCaptcha won't catch its own empty field
matt w's Avatar
Skilled Talker

Posts: 80
Location: kalamazoo
VirtuosiMedia,

I added this code:

HTML Code:
$answer = $_POST['recaptcha_response_field'];
     $answer = trim($answer);
     if(empty($answer)){
The empty captcha flies right through, same as before.

Thanks,
mw
matt w is offline
Reply With Quote
View Public Profile Visit matt w's homepage!
 
Reply     « Reply to reCaptcha won't catch its own empty field
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.09165 seconds with 12 queries