• i believe in what i do •

August 2013 Archives

reCaptcha in 3 Steps - PHP Form

| 0 Comments | 0 TrackBacks
Here are the three simple steps to integrate google reCaptcha in your website forms (example: content or comments) Here is the sample code.

<?php //---------------- require_once('recaptchalib.php'); // Get a key from https://www.google.com/recaptcha/admin/create $publickey = "<enter your reCaptcha publickey>"; $privatekey = "<enter your reCaptcha privatekey>"; # the response from reCAPTCHA $resp = null; # the error code from reCAPTCHA, if any $error = null; if ($_POST["recaptcha_response_field"]) { $resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]); if ($resp->is_valid) { echo "You got it!"; $to = "yourMail@gmail.com"; $subject = "newUserContact"; $body = $_POST["comments"]; $from = $_POST["email"]; mail($from, $subject, $body, $from); // } else { # set the error code so that we can display it $error = $resp->error; } } ?> <form action="" method="post" name="check_form"> <table width="315" border="0" cellspacing="0" cellpadding="5" bordercolor="#FFFFFF"> <tr> <td bgcolor="#FFFFFF"><strong>Name: * </strong><br> <input name="name" type="text" id="name2" size="30" /></td> </tr> <tr> <td><strong>eMail:*</strong> (Will not appear online)<br> <input name="email" type="text" id="email3" size="30" /></td> </tr> <tr> <td><strong>Comments:</strong><br> <textarea name="comments" cols="28" rows="5" id="textarea2"></textarea></td> </tr> <tr> <td><?php echo recaptcha_get_html($publickey, $error); ?></td> </tr> <tr> <td><div align="right"> <input name="submit" type="submit" value="Comment"/> &nbsp;&nbsp; </div></td> </tr> </table> </form>
Click here to view the sample page