Building a complete, jQuery-based, solution for stopping spammers from mis-using your forms.

Original: http://15daysofjquery.com/safer-contact-forms-without-captchas/11/
Author: Jack Born

Similar Tutorials: AjaxManipulationForms

Here’s a subject that’s close to my heart: secure contact forms.

As I mentioned in a previous tutorial, one common use of contact forms is to help visitors communicate with you without exposing your email address to the email harvesting software used by spammers.

But when it comes to spam, hardly anything is worse than an insecure contact form. Imagine getting a nasty note from your web hosting company that your site has been used to send out massive amounts of email about black market erection medication and, oh by the way, your site is offline until you get it fixed - thank you very much.

So what I’m going to show you today is a simple method for adding an extra layer of security to ANY contact form on your site.

Contents

[edit]

The Situation

You notice that spammers have been remotely probing your contact forms for vulnerabilities… and you want them to quit.

[edit]

The Problem

You don’t want to use CAPTCHA’s (Completely Automated Public Turing Test to Tell Computers and Humans Apart) because you just know that requiring your visitors to read squiggly letters and numbers just to send you a message is going to suppress communication - not encourage it.

An example CAPTCHA

Bottom line: you want to make life tough for the Bad Guys and super simple for the Good Guys.

[edit]

The Solution

You’re going to use jQuery to add some hidden tag information to the contact form when the page loads. When the form is sent to the processor, you’ll use some simple PHP code to verify the following:

In other words, your visitor will have a limited amount of time (specified by you) to fill in the form and send it. And if a spammer tries to post information to your form processor remotely they’re going to hit a big fat roadblock. Do not pass go, do not collect $200.

What I’m going to share with you is a modified concept I read from a very smart fellow named Chris Shiflett. He’s a security expert on all sorts of issues that PHP programmers can come across if they’re not careful.

[edit]

The Tutorial

I got great responses from the tutorial for Table Striping Made Easy so I’ve decided to do another “walk through” tutorial with screen grabs. It’s a little time consuming, but hey, you’re worth it.

[edit]

The Idea

Specifically...

So let's get rolling... first we need a contact form.

[edit]

Simple Contact Form:

Image:Form2.png
Next... bring on the jQuery

[edit]

Grab the jQuery Library

Image:Form3.png
And kick start the rest of the code with $('document').ready()
Image:Form4.png
Next... remove the "javascript required" warning

[edit]

Consider If Your Visitor Isn't Using JavaScript

[edit]

PHP File

Image:Form7.png

[edit]

Ajax Call

Our JavaScript will use jQuery's $.get(file, callbackFunction) for the Ajax call. This will do a simple GET request to the server to retrieve the text.

Image:Form8.png

[edit]

jQuery Appends the Server Time

Use append() to tell jQuery to add a hidden input to the form just before the closing form tag.

Image:Form9.png
Image:Form10.png
Image:Form11.png

[edit]

Checking the Form

test.php is the example PHP code used to verify the token

Image:Form12.png

[edit]

Prevent Caching

Image:Form13.png

[edit]

What makes this solution better?

Chris Shiflett's code required that the contact form page be written in PHP

[edit]

What makes this solution worse?

[edit]

Final Analysis

[edit]

Demo and Code

[edit]

Silver Bullet?

“So now my forms are 100% secure and I can use Generic Free Contact Form Processor With Sloppy Code and feel safe?”

Uh… no.

This security concept is based on a key assumption:

Spammers would prefer to go after the “low hanging fruit” than spend all day trying to crack a tricky contact form.

Now listen carefully, dear friend:

This technique, although strong, is not a cure for a weak form processor.

My attitude towards securing a contact form is to use multiple methods on both the server side and the client side so that a spammer is going to have to invest an enormous amount of resources to even come close to succeeding with their evil plot.

I view the client side protection as analogous to posting a sticker on the windows of a home to indicate that the house is wired with an alarm. Thieves know to look for stickers, dogs in the yard, lights on the exterior of a home, and other signs of a well guarded house. They’re looking for high payoff with minimal work and risk.

In other words, if you can thwart 99% of attacks before they really get started and you can do it so easily why wouldn’t you? That’s what this technique does.

But it’s not a silver bullet cure for a crappy form processor.