Using Recaptcha code

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

Hi

I wanted to add the google recaptcha code to an enquiry form I have.

The code for the form in headers I have is:

<?php header('Content-type: text/html; charset=utf-8'); ?>

<?php

  /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */

  // load viewer library

  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';

  $dirsToCheck = array('/home/domain/public_html/domain.com/','','../','../../','../../../');

  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}

  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

   list($officeRecords, $officeMetaData) = getRecords(array(

    'tableName'   => 'office',

  ));

  // load records

// process form

  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"; }

    // send email user

    if (!$errorsAndAlerts) {

      $from    = $_REQUEST['email'];

      $to      = "info@domain.com";

      $subject = "Email form submitted";

      $message = <<<__TEXT__

You've received an email enquiry

Full name: {$_REQUEST['fullname']}

Country: {$_REQUEST['country']}

Phone: {$_REQUEST['telephone']}

Email:     {$_REQUEST['email']}

Message:   {$_REQUEST['message']}

The user who sent this message had the IP address {$_SERVER['REMOTE_ADDR']}.

__TEXT__;

      // Note: The above line must be flush left or you'll get an error

      // This is a PHP heredoc.  See: http://ca2.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

      // send message

      $mailResult = @mail($to, $subject, $message, "From: $from");

      if (!$mailResult) { die("Mail Error: $php_errormsg"); }

      //

       redirectBrowserToURL('/confirm.php?success='.urlencode('Thank you for your enquiry. '));

    $_REQUEST = array(); // clear form values

    exit;

    }

  }

?>

The first line on the form is:

<form method="post" action="?">

The instructions on recaptcha are to change this to:

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

The code for verify.php is:

<?php

  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

    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .

         "(reCAPTCHA said: " . $resp->error . ")");

  } else {

    // Your code here to handle a successful verification

  }

  ?>

At the point where is says // Your code here to handle a successful verification do I need to copy some of the code I have in my header over to verify.php? If so what should I copy over?

Thanks