Using Recaptcha code

2 posts by 2 authors in: Forums > CMS Builder
Last Post: September 5, 2013   (RSS)

By Jason - September 5, 2013

Hi,

You'll want to use the code you have after the "// process form" after you've checked the recaptcha code.

A good approach would be something like this:

 if (@$_REQUEST['submitForm']) {

    // error checking

    $errorsAndAlerts = "";

    if (!@$_REQUEST['fullname'])                { $errorsAndAlerts .= "You must enter your full name!<br/>\n"; }

    if (!@$_REQUEST['email'])                   { $errorsAndAlerts .= "You must enter your email!<br/>\n"; }

    else if(!isValidEmail(@$_REQUEST['email'])) { $errorsAndAlerts .= "Please enter a valid email (example: user@example.com)<br/>\n"; }

               if (!@$_REQUEST['country'])                 { $errorsAndAlerts .= "You must enter a value for country!<br/>\n"; }

               if (!@$_REQUEST['message'])                { $errorsAndAlerts .= "You must enter a message!<br/>\n"; }

  require_once('recaptchalib.php');

  $privatekey = "your_private_key";

  $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

    $errorsAndAlerts .= "The reCAPTCHA wasn't entered correctly. Go back and try it again.";


  } 

In this way, the recaptcha checking becomes part of the normal error checking on the form.

Hope this helps.

---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/