How to Add a Completely Custom Captcha to Any Web Form

Confusing Captcha

As promised in my previous article, I’ll show you how to add a captcha to your form. Since those askew combinations of letters and numbers can be difficult to decipher for some people, this one is going to be in the form of a basic intellectual question.

It’s fairly simple, just write a question in your HTML, provide an input field, the PHP file checks what’s typed in, and if the user hasn’t entered the correct answer, the inputted data isn’t processed. You don’t get spam. The spammer quits spamming. Now, the Internet is a better place, thanks to you. Well, maybe that’s a bit of an exaggeration. ;)

First, in your HTML file, paste this in the place you’d like it to appear on the page; typically just before the Message or the Send button.

<p>2 + 3 =</p>
<input type="text" name="captcha" />

If you want to use a different math problem or question, replace 2 + 3 = with it. You can really use whatever you want, the first name of the current president would work, for example.

Now add the proper PHP to process this. Assuming you’re using the mailer.php I provided in the last post, paste this in that file after the 4 lines that check to make sure the Name, Subject, Email, and Message were entered correctly.

$captcha = check_input($_POST['captcha']);

Next, after the code that verifies the email address, paste in this:

if (!preg_match("/5/", $captcha))
{
show_error("Check your math, Dude");
}

Since my question is 2 + 3 =, you’ll see I’ve set my answer as 5, of course. If you’re using any other question, be sure and put it’s proper answer where the 5 is. If somebody puts in anything other than the numeral 5, they’ll be greeted with Check your math, Dude. Change that error message to something that matches your question.

You’re finished! Congratulations, you’ve added a completely custom captcha to your email contact form without the help of any third-party generator.

If it’s not working for you, just ask for help in the comments, and we’ll see if I can help get yours up and running.

If you enjoyed this article, why not share it with your friends? I’d appreciate it.

Get this delivered to your inbox!

It's quick, free, and painless.

Comments

  1. Lol dude I was expecting to see the Google captcha. You should do that next. That’s a tricky one. :D

    This validation is really powerful though. I haven’t gotten ANY spam since I implemented mine. It’s awesome!

    Great job. I think the Genesis transition has made your site more evenly proportioned, particularly the typography.

    • Like the confusing kind I show in the picture? I actually kind of like this kind better. ;)

      Thanks, I’m glad to hear that. It helps when you write all of the CSS rather than edit and add stuff to something written by somebody else. Everything matches better.

      Also I bumped up the font size to the famed 16px and used Chris Pearson’s Golden Ratio Typography Calculator.

  2. Topspeed says:

    I really like this post.

    Could it be implemented on my form page here:

    http://www.topspeedracer.com/topspeed-fan-club.html

  3. Chris Mitchell says:

    You are amazing! I have been searching and searching online for an easy captcha (I’m still green to coding) and this was the easiest one that I’ve found, and it works perfectly. Thanks so much!!!

  4. Raymond says:

    the mailer doesnt seem to work.. When i press send it shows internet site not found..

    Im still a bit green with all this.. Seems so easy but it isnt…

    • Do you still receive the email from the form? If so, you just need to verify the path to the page that mailer.php directs the user to after submitting the form. (In this case, thanks.html)

  5. Cesar says:

    Hi, I’m getting the following error any idea?

    Warning: Cannot modify header information – headers already sent by (output started at /home/content/29/10106029/html/base/mailer.php:1) in /home/content/29/10106029/html/base/mailer.php on line 37

  6. Leon says:

    Hi I love your tutorials,

    I tried to implement this captchaon my site:
    http://leongaban.com/ (contact form at the bottom)

    However it seems like validation doesn’t work, do you have a tutorial about inline HTML error messages? Also the now with the captcha in the code I don’t get my thank you page after sending an email :o

  7. Leon says:

    Oh sorry, my bad! I accidentally uploaded an older version of my file. The older version did not have the mailer.php linked too in the form, lol…

  8. Melissa says:

    Will I be able to add your captcha to my page?
    http://eatonpolice.org/bicycle-registration.html

    I’m new to this still trying to figure out where to add it to?

    Thanks,
    melissa

    • Daniel says:

      Certainly! I see a form with a captcha at the bottom of the linked page – Is this non-functional and needs fixing or do you want to replace the form entirely?

  9. Melissa says:

    Ok I placed it on all the forms we have and it is not working at all. I’m still getting spam. I had emailed back stating I only used the addition problem because when I placed all the info it showed code after the box where you enter the answer to the math problem. If you have another email address I can send screen shots.

  10. Fred says:

    Will this work with a cgi-based form handler, such as Alien Form? (Yeah, I know it’s outdated, but I have it on so many sites, it would be nice to just go in and add a captcha in there).

    See, for example, the form located here:

    http://www.northsidefalcons.com/feedback.htm#email

    …which points to the cgi script here:

    http://www.northsidefalcons.com/cgi-bin/af2.cgi

  11. Vern says:

    I also have a .cgi form. Will it work?

  12. Steve says:

    Great tutorial. But something doesn’t seem to be working right on my end. When I fill out the form and submit, no email is received. Any tips?

Speak Your Mind

*