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. 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. 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. 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. 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. 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. 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

    • 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. 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. 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. I also have a .cgi form. Will it work?

  12. 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?

  13. Daniel, you helped me A LOT with this straightforward approach.
    No tinky-winky-bs on your form and captcha tutorial.
    Thanks.

  14. I am a private loan lender that give out loans online, all at an affordable interest rate of 3% and a long-term repayment of the period, which is negotiable, so if you are in need of a loan, all you need to do , contact me via email wakintongeorge01@hotmail.com for more information on how to get the loan.

  15. Hello and thank you for your information it
    is greatly appreciated.

    I have got the PHP email
    form with added fields and subject line working
    on my site. As well as the custom Captcha.

    I was wondering if there is a way to have the
    email from the form be sent from a real email address
    instead of anonymous@domain.com
    Thank you

  16. Hello, Im getting the error below.

    Fatal error: Call to undefined function check_input() in /home/swingfoc/public_html/test/contact.php on line 21

  17. Hi Daniel, thanks for the tutorial, it is very helpful and understandable.
    I have a slight problem with my form, I don’t recieve any emails when I’m submitting the message and there is no errors, do you have any ideas?
    Thank you!

  18. Hi!

    I’m very interested in using something like this to stop wordpress spam!

    It’s really surprising that there are no good WordPress custom captchas.

    Do you think it would be hard to implement this on a wordpress site?

  19. asd

  20. I added a form to our works website last week and it was getting about 100 spam emails a day. Now I have added your bit of code and it seems to have stopped. It was very easy to add the code. Thanks Very Much Daniel

  21. How do I implement this into a wordpress site?
    I created the mailer.php, uploaded into theme folder and also main folder, but it seems like:

    Is not calling the mailer.php.

  22. OK – adding the code for numerical question and answer block does make it appear on the uploaded to server contact page.

    Adding the code to process that (step 2 in the instructions)doesn’t seem to cause any errors, and an upload page to server check still shows the Q & A question etc.

    Stumped on step 3 “Next after the code that verifies the email address, paste in this” (followed by the 4 lines as per the instructions”. Tried a few places but each causes the page not to display when logging to the site and attempting to open the Contacts link. Just a blank white screen is displayed.

    The server apparently supports PHP because the contact web form PHP section does work – I want to add some security to it.

    Can I post up the relevant PHP section of the Contact page here for comment – or may someone offer to take a look at it if I email this to them?

Speak Your Mind

*