You’ll have noticed captchas on many of the sites you use. When you try to send a contact message, or sign up to a new website, you’re asked to type the text you can see in a distorted picture, to check you really are a human being, not a robot.

captcha letter recognition test
But to keep up with letter recognition technology, Google has come up with a simplified test, called reCAPTCHA. In this test, you just tick a box and that is enough to prove you’re made of flesh and blood. If you make a mistake the first time, it may offer you another tickbox test to distinguish pictures from each other. It’s a pleasingly simple solution that’s easy to integrate into your contact form.

Re-captcha – it’s as easy as ticking a box.
Get started by collecting your reCAPTCHA site and secret keys from Google
Go to https://www.google.com/recaptcha/admin and sign in using your google account. Register the site, and get the two keys – site key and secret key – that you’ll be needing later for the construction of your contact form.

screenshot google reCAPTCHA keys
Now you’re ready to build your form.
Add the reCAPTCHA snippet inside the head tag of your contact page
head of contact.php
1 |
<script src="https://www.google.com/recaptcha/api.js"></script> |
Build your form in html
body of contact.php.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php if(!empty($succMsg)): ?> <?php echo $succMsg; ?> <?php endif; ?> <?php if(!empty($errMsg)): ?> <?php echo $errMsg; ?> <?php endif; ?> <strong>email</strong><br> <form action="" method="POST"> <input type="text" value="<?php echo !empty($contact_name)?$contact_name:''; ?>" placeholder="your name" name="contact_name" required> <?php //required attribute makes field required in html 5 browser, but may not work in safari?> <input type="text" value="<?php echo !empty($email)?$email:''; ?>" placeholder="your email address" name="email" required> <textarea type="text" placeholder="message..." required name="message" > <?php echo !empty($message)?$message:''; ?></textarea> <div class="g-recaptcha" data-sitekey="YOUR SITE KEY HERE"></div> <input type="submit" name="submit" value="send message"> </form> |
Make sure you’ve added your reCAPTCHA site key to the contact form, just above the input button
1 |
<div class="g-recaptcha" data-sitekey="YOUR SITE KEY HERE"></div> |
Create your php include which controls the reCAPTCHA and other form validation
includes/contact_verif.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<?php function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } if(isset($_POST['submit'])): if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])): $secret = 'YOUR SITE SECRET KEY HERE'; $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']); $responseData = json_decode($verifyResponse); $contact_name = !empty($_POST['contact_name'])?$_POST['contact_name']:''; $contact_name = test_input($contact_name); $email = !empty($_POST['email'])?$_POST['email']:''; $email = test_input($email); $message = !empty($_POST['message'])?$_POST['message']:''; if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))://make sure the email address is valid $email = $email; if($responseData->success): //contact form submission code $to = 'YOUR WEBSITE EMAIL ADDRESS HERE'; $subject = 'Website contact form'; $htmlContent = " <p><b>Name: </b>".$contact_name."</p> <p><b>Email: </b>".$email."</p> <p><b>Message: </b>".$message."</p>"; //set content-type for sending HTML email $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; $headers .= 'From:'.$contact_name.' <'.$email.'>' . "\r\n"; //send email @mail($to,$subject,$htmlContent,$headers); $succMsg = 'Thank you. Your message has been sent.'; $contact_name = ''; $email = ''; $message = ''; else: $errMsg = 'Robot verification failed, please try again.'; endif; else: $errMsg = 'there is a problem with your email address'; endif; else: $contact_name= $_POST['contact_name'];//re-display content if error $email= $_POST['email'];//re-display content if error $message= $_POST['message'];//re-display content if error $errMsg = 'Please click on the reCAPTCHA "I\'m not a robot" box.'; endif; else: $errMsg = ''; $succMsg = ''; $contact_name = ''; $email = ''; $message = ''; endif; ?> |
Make sure you’ve added your reCAPTCHA secret key inside the $secret variable, about line 10
1 |
$secret = 'YOUR SITE SECRET KEY HERE'; |
Add your include to the contact page, above the head tag
contact.php
1 2 3 |
<?php include('includes/contact_verif.inc.php'); ?> |
Now you’re ready to receive some contact.

Have they made contact yet? Crop Circle by John Evans