midrange.com code scratchpad
Name:
PHP Example using reCAPTCHA
Scriptlanguage:
PHP
Tabwidth:
4
Date:
01/08/2009 07:53:52 pm
IP:
Logged
Description:
This is an example of using reCAPTCHA from PHP. You'll need to download a library and get the public and private keys from http://recaptcha.net/
Code:
  1. <html>
  2.  
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5. <title>MattsOldCars.com - Talk To Matt</title>
  6. <link rel="stylesheet" href="../common/mattsoldcars.css" type="text/css">
  7. </head>
  8.  
  9. <body style="margin:0px;">
  10. <?php
  11.  
  12. require_once('recaptchalib.php');
  13. $publickey = "xxx";
  14. $privatekey = "yyy";
  15. $errors = array("","","","","");
  16. $numErrors = -1;
  17.  
  18. if($_POST["action"] == "Send Email") {
  19.     if($_POST["name"] == "") {
  20.         $numErrors += 1;
  21.         $errors[$numErrors] = "Please enter your name.";
  22.     }
  23.     if($_POST["email"] == "") {
  24.         $numErrors += 1;
  25.         $errors[$numErrors] = "Please enter your email address.";
  26.     }
  27.     if($_POST["subject"] == "") {
  28.         $numErrors += 1;
  29.         $errors[$numErrors] = "Please enter a subject.";
  30.     }
  31.     if($_POST["message"] == "") {
  32.         $numErrors += 1;
  33.         $errors[$numErrors] = "Please enter a message.";
  34.     }
  35.     $resp = recaptcha_check_answer ($privatekey,
  36.                                 $_SERVER["REMOTE_ADDR"],
  37.                                 $_POST["recaptcha_challenge_field"],
  38.                                 $_POST["recaptcha_response_field"]);
  39.  
  40.     if (!$resp->is_valid) {
  41.         $numErrors += 1;
  42.         $errors[$numErrors] = "Sorry, you don't appear to be human.";
  43.     }
  44.  
  45.     if ($numErrors < 0) {
  46.         $subject = "[Contact Form]" . $_POST["subject"];
  47.         $from = "From: " . $_POST["name"] . " <" . $_POST["email"] . ">\r\n";
  48.         
  49.         // split message body into shorter lines
  50.         $long_lines = split("\n", $_POST["message"]);
  51.         $split_text = ""; 
  52.         $width = 78; 
  53.         foreach ($long_lines as $oneline) { 
  54.                $split_text = $split_text . wordwrap($oneline, $width, "\n") . "\n"; 
  55.         }
  56.         // $split_text will have one more newline at the end than what you 
  57.         // started with, so let's clean it up 
  58.         $split_text = substr($split_text, 0, -1);
  59.         
  60.         mail("you@yourdomain.tld", $subject, $split_text, $from);
  61.     }
  62.  
  63. }
  64.  
  65. ?>
  66.  
  67. <table width="100%" border="0" cellpadding="0" cellspacing="0">
  68.   <tr>
  69.     <td colspan="3" class="titlebar"><h1 class="sitetitle">Matt's Old Cars.com</h1>
  70.     <h2 class="sitetagline">Fiddling with Rambler's since 1995</h2>
  71.     </td>
  72.   </tr>
  73.   <tr>
  74.     <td class="navbar" width="23%"><?php include("../navigation.txt"); ?>
  75.     <p>Send me an e-mail:<br>
  76.     &nbsp; <?php include("../common/emailaddr.txt"); ?><br>
  77.     </td>
  78.     <td width="2%"></td>
  79.     <td class="contents"><span class="sectiontitle">Talk To Matt</span>
  80. <?php
  81.     if ($numErrors > -1 ) {
  82.         echo "<p><span class=\"warning\">Please correct the following:</span></p>";
  83.         echo "<ul>";
  84.         for ($x=0; $x <= $numErrors; $x++) {
  85.             echo "<li class=\"warning\">" . $errors[$x] . "</li>";
  86.         }
  87.         echo "</ul>";
  88.     }
  89.  
  90. if($_POST["action"] != "Send Email" || $numErrors >=  0) {
  91. ?>
  92. <p>Due to 
  93.     the large amount of spam I get, I'm no longer publishing an email address 
  94.     on this web site. Please use the form below to send me an email. All of the 
  95.     items on this form are required.<br>
  96. &nbsp;<form method="POST" enctype="multipart/form-data">
  97.       <table border="0" cellpadding="2" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
  98.         <tr>
  99.           <td valign="top">Your Name:</td>
  100.           <td><input type="text" name="name" size="35" tabindex="1" value="<?php echo $_POST["name"]; ?>"><span class="example"> example: 
  101.           Alan Smithy</span></td>
  102.         </tr>
  103.         <tr>
  104.           <td valign="top">Your Email Address:</td>
  105.           <td><input type="text" name="email" size="35" tabindex="2" value="<?php echo $_POST["email"]; ?>"><span class="example"> example: 
  106.           alan.smithy@example.com</span></td>
  107.         </tr>
  108.         <tr>
  109.           <td valign="top">Subject:</td>
  110.           <td><input type="text" name="subject" size="62" tabindex="3" value="<?php echo $_POST["subject"]; ?>"></td>
  111.         </tr>
  112.         <tr>
  113.           <td valign="top">Your Message:</td>
  114.           <td><textarea rows="14" name="message" cols="55" tabindex="4"><?php echo $_POST["message"]; ?></textarea></td>
  115.         </tr>
  116.         <tr>
  117.           <td valign="top">Verify that you're a human:</td>
  118.           <td><?php echo recaptcha_get_html($publickey); ?></td>
  119.         </tr>
  120.       </table>
  121.       <p><input type="submit" value="Send Email" name="action" tabindex="100"></p>
  122.     </form>
  123. <?php
  124. else {
  125. ?>
  126. <p>Your message has been sent!</p>
  127. <?php
  128. }
  129. ?>
  130.     <p><br>
  131.     <p align="right" class="tiny">Last Updated 
  132.     <!--webbot bot="Timestamp" s-type="EDITED" s-format="%m/%d/%y %I:%M:%S %p" startspan -->02/03/08 02:50:17 PM<!--webbot bot="Timestamp" i-checksum="27495" endspan --> </td>
  133.   </tr>
  134. </table>
  135.  
  136. </body>
  137. </html>
© 2004-2019 by midrange.com generated in 0.022s valid xhtml & css