Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PHP/HTML: I\'m trying to add capcha recapcha to my contact form but I can\' t ge

ID: 3791198 • Letter: P

Question

PHP/HTML: I'm trying to add capcha recapcha to my contact form but I can' t get it working. Could anyone advise?

<?php include 'includes/header.php'?>
<h3>Welcome</h3>
  

<?php
  
  
define('THIS_PAGE' ,basename($_SERVER['PHP_SELF']));

if(isset($_POST['Name'])) //the index name must match the name attritube below
{
// *** show feedback after submisison ***
  
/*
echo '<pre>';
var_dump($_POST);
echo '</pre>';
die;
*/
  
$message = process_post();
$email = $_POST['Email'];
//echo $myText;
  
$to = 'test@test.com';
$subject = 'Test Email';
//$message = 'hello';
$headers = 'From: noreply@server.com' . PHP_EOL .
'Reply-To: ' . $email . PHP_EOL .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
  
echo '<p>Thank you. We will be in touch within 24 hours. We promise.</p>';
}
else
{
// *** show form ***
echo '
<form action="' . THIS_PAGE . '" method="post" >
  
   <fieldset>
   <legend>Contact Information</legend>
  
       <br><label>Name</label><br>
<input type="name" name="Name" required placeholder="Name">
      
       <br><label>Email</label><br>
<input type="email" name="Email" required placeholder="Email">      
       <br><label>Phone</label><br>
  
       <input type="text" name="phone" placeholder="Optional">
  
   </fieldset>   <!--Close Personal Info box -->
      
      
   <fieldset>
   <legend>What would you like to know more about?</legend>

       <p><input type="checkbox" name="moreinfo[]" value="schedule-an-appointment"> Schedule An Appointment </p>
       <p><input type="checkbox" name="moreinfo[]" value="transfer-from-another-docter"> Transfer From Another Doctor </p>
       <p><input type="checkbox" name="moreinfo[]" value="questions-about-my-health-insurance"> Questions About My Health Insurance </p>
       <p><input type="checkbox" name="moreinfo[]" value="request-a-follow-up"> Request A Follow-up </p>
       <p class="last"><input type="checkbox" name="moreinfo[]" value="Something-else"> Something else </p>  
   </fieldset> <!-- Close About Section-->

  
   <fieldset>
   <legend>How did you hear about us?</legend>
  
       <select name="how">
       <option>Choose One</option>
       <option value="word-of-mouth">Word of mouth</option>
       <option value="referral">Referral</option>
       <option value="google">Google</option>
       <option value="neighborhood-welcome-packet">Neighborhood Welcome Packet</option>
       </select>
  
   </fieldset> <!-- Close How Box -->
  
  
   <fieldset>
   <legend>Give us a few words that more describes your concern.</legend>
       <textarea name="comments" rows="8"></textarea>
      
   </fieldset>  
  
<input type="submit" value="Submit">

</form>
';
}

function process_post()
{//loop through POST vars and return a single string
$myReturn = ''; //set to initial empty value

foreach($_POST as $varName=> $value)
{#loop POST vars to create JS array on the current page - include email
$strippedVarName = str_replace("_"," ",$varName);#remove underscores
if(is_array($_POST[$varName]))
{#checkboxes are arrays, and we need to collapse the array to comma separated string!
$myReturn .= $strippedVarName . ": " . implode(",",$_POST[$varName]) . PHP_EOL;
}else{//not an array, create line
$myReturn .= $strippedVarName . ": " . $value . PHP_EOL;
}
}
return $myReturn;
}


?>

<?php include 'includes/footer.php'?>

Explanation / Answer

If you want to use the PHP library to display the reCAPTCHA widget, you'll need to insert this snippet of code inside the <form> element where the reCAPTCHA widget will be placed:

require_once('recaptchalib.php');
$publickey = "your_public_key"; // you got this from the signup page
echo recaptcha_get_html($publickey);

With the code, your form might look something like this:

<html>
<body> <!-- the body tag is required or the CAPTCHA may not show on some browsers -->
<!-- your HTML content -->

<form method="post" action="verify.php">
<?php
require_once('recaptchalib.php');
$publickey = "your_public_key"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
<input type="submit" />
</form>

<!-- more of your HTML content -->
</body>
</html>

Don't forget to set $publickey by replacing your_public_key with your API public key.

Note that the value of the "action" attribute is "verify.php". Now, verify.php is the destination file in which the values of this form are submitted to. So you will need a file verify.php in the same location as the client html.

The require_once function in the example above expects recaptchalib.php to be in the same directory as your form file. If it is in another directory, you must link it appropriately. For example if your recaptchalib.php is in the directory called "captcha" that is on the same level as your form file, the function will look like this:require_once('captcha/recaptchalib.php').

Server Side (How to test if the user entered the right answer)

The following code should be placed at the top of the verify.php file:

<?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
}
?>

In the code above:

Notice that this code is asking for the private key, which should not be confused with the public key. You get that from the same page as the public key.

Also make sure your form is set to get the form variables using $_POST, instead of $_REQUEST, and that the form itself is using the POST method.

That's it! reCAPTCHA should now be working on your site.