Jump to content

[SOLVED] ReCaptcha and Form Submit


kagedwebdesign

Recommended Posts

Hi everyone!

 

I am a long time user of HTML, and just made the jump into learning how to code PHP.  I'm doing pretty well, but I am running into a problem with a Captcha program i'm using...

 

I am using ReCaptcha, which uses their servers to check everything...using a couple different cut and paste codes...

 

It's not having any problem verifying if it is correct or not...the problem comes in after it verifies the form, i'm not sure how to get it to send out...I put the codes below...

 

The Form Code:

<form action="verify.php" method="post" name="f1">

    <input type="hidden" name="subject" value="update subscription"/>
    <input type="hidden" name="redirect" value="signup2.html" />
    <p align="center">If you would like to receive updates from Living The Word Ministries, enter your name, e-mail, and check the appropriate box.<br>
      <label>      </label>
  </p>
<p align="left">      <br>
      Name: 
      <input type="text" name="name" id="name">
      <br>
    E-mail Address:
      <input type="text" name="email" id="email">
      <br />
      <label>
      <input type="checkbox" name="Tuesdays" id="options" />
      Tuesdays With Jesus<br />
      </label>
      <input type="checkbox" name="Updates" id="options" />
      Updates<br>
      <br />

<!-- RECAPTCHA CODE -->
<?php
require_once('recaptchalib.php');
$publickey = "6Lc2IwcAAAAAAOY4EezqClpUGH0WW7X4PTL4pYHy"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
<!-- RECAPTCHA CODE END -->


<br>
<input type="submit" value="Submit" /><br />
  </p>
    </form>

 

VERIFY.PHP

<?php
require_once('recaptchalib.php');
$privatekey = "6Lc2IwcAAAAAAHe3xlShgFSzziEnhLBw-14nz0nP";
$resp = recaptcha_check_answer ($privatekey,
                               $_SERVER["REMOTE_ADDR"],
                               $_POST["recaptcha_challenge_field"],
                               $_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
   // What happens when the CAPTCHA was entered incorrectly
   die ("The CAPTCHA wasn't entered correctly. Go back and try it again." .
        "(reCAPTCHA said: " . $resp->error . ")");
} else {
     Echo ("Your Form is being submitted.  You will be re-directed shortly.");
}
?>

 

 

I do not want it to echo that, but rather send the contents of all the entries from the form.  It uses GoDaddy's included cgi called "/cgi/gdform.cgi"  It works fine when i do not run the captcha, so I know the problem isn't in the CGI file.  When i try to forward the page automatically using a meta refresh code, it tells me that it cannot submit it using /cgi/gdform.cgi...

 

I'm really quite clueless, so any help would be greatly apprecaited!!

 

Thank you!

 

Kyle Schultz

Kaged Web Design

Link to comment
https://forums.phpfreaks.com/topic/164817-solved-recaptcha-and-form-submit/
Share on other sites

The problem is that after you verify the captcha in the PHP code and redirect to GoDaddy's cgi script used to send the email, the cgi script does not get the values posted by the form. Only the first place you post the form to will get the values of the form.  http://phpmailer.worxware.com/ has a good piece of software called PHP-Mailer FE that makes it easy to email posted forms. There are some examples in the 'examples' folder that you can learn from.  You would want to verify the captcha and send the email all before redirecting anywhere.

I was able to re-do the code, and it works almost without issue...the code for the actual form itself is the same (only now it's sending to an outside code called sendmail1.php).  the only issue i have now is that it validates everything, but once it validates the CAPTCHA, it doesn't matter if everything else was valid, it just goes...i posted to code below

 

<meta http-equiv="refresh" content="1;url=thankyoupage.html">

<?php

$name = htmlentities($_POST['name']);
$email = htmlentities($_POST['email']);
$message = htmlentities($_POST['message']);
$current = htmlentities($_POST['current']);
$from = htmlentities($_POST['email']);
$body = "Name = $name\r Email = $email\r Current = $current\r Message = $message";

if (!$name)
{
  echo "Please enter your name.";
}
else if (ereg("<", $name))
  {
   echo "No HTML is allowed.";
  }
else if (ereg(">", $name))
  {
   echo "No HTML is allowed.";
  }
else if (!$email)
  {
   echo "Please enter your email.";
  }
else if (!ereg("@", $email))
  {
   echo "Please enter a valid email.";
  }
else if (ereg("<", $email))
  {
   echo "No HTML is allowed.";
  }
else if (ereg(">", $email))
  {
   echo "No HTML is allowed.";
  }
else if (!$message)
  {
   echo "Please enter a message.";
  }
else if (ereg("<", $message))
  {
   echo "No HTML is allowed.";
  }
else if (ereg(">", $message))
  {
   echo "No HTML is allowed.";
  }
else if (!ereg("www.",$current))
  {
   echo "Please enter a valid website address.";
  }
  
  require_once('recaptchalib.php');
$privatekey = "6LeqMwcAAAAAALGIqQeFjPvK23qtuu8y1vkdsn2x";
$resp = recaptcha_check_answer ($privatekey,
                               $_SERVER["REMOTE_ADDR"],
                               $_POST["recaptcha_challenge_field"],
                               $_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
   // What happens when the CAPTCHA was entered incorrectly
   die ("The CAPTCHA wasn't entered correctly. Go back and try it again."); } 
else
  {
   mail('[email protected]', 'Free Quote Contact Form', $body, $from);
   echo ("Form Submitted...you will be redirected shortly.");
   
     }

?>


 

not quite sure where to go from here...:-\ I'm getting close, i'm not getting any errors...:)

There are a few issues I see with this implementation.  1)The meta refresh at the top of the page.  If you want to redirect the user to thankyoupage.html, verify that both the captcha is valid AND the user filled out the form correctly, and then use the PHP call header("Location:http://www.myurl.com/thankyoupage.html");.  So you'll want to use an $error variable that you set to true if there was an error filling out the form, and then check that value before sending the email.  If there was an error, don't try to send the mail.  Note that if you are using the function header(), it has to be done before you send any output to the browser. 2)You call htmlentities on $name, $email, and $message but then you look for the < and > characters, which won't be able to exist after calling htmlentities on them. 3)Your email validation won't actually verify the email is correct, it could be the symbol @ and pass muster, but that's okay if you're happy with it. 4)You require www. to determine if the website address is valid, but some domains actually don't work, for one reason or another, with www. Probably very few, and it's usually because of a server side error, but an issue to think about.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.