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.


  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:

  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: (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?

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


    • 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:

    …which points to the cgi script here:

  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.

  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 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
    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. There are only specific instances when inquiries will appear.
    One of the biggest mistakes annd money wasters, is
    many consumers mistakenly believe that just paying old debts will improve
    their credit. If tthe default, but you’re not the only one who
    suffers. If you have existing collection accounts, now is the
    time to handle them. 2 upwards and can even opt for a more comprehensive
    report. In general, they reduce the costs
    upfront, but then drag the process forever to offset their “attractive” teaser rates.
    Your a poor credit rating won’t whatsoever come if the involves
    getting an automobile of the selecting just
    just just in case opting for guaranteed Dallas Texas a poor credit rating vehicle loan.
    If you have collections activity, verify that it’s legitimate, remedy the account, or if it is not legitimate, work
    with the credit reporting agency to remove the information.
    Since one’s worthiness to receive loans entirely depends upon a free credit report,
    it’s essential to contact one of the best in the business of online report analyzers, who can
    help you achieve perfect credit scores by analyzing and summarizing
    your financial history.

  23. I find it pretty ironic that an article about adding captchas to webforms has a comment form that doesn’t have a captcha, and guess what, there are a few spam comments posted about credit scores & video games. ;-)

  24. How would I make the user land on a specific page upon completion that says “thank you for your email.” or on an error page when the do something wrong?

  25. HI,

    I get a 500 internal server error?
    Please help!!
    I run this on our webserver that hosts websites.


  26. I am trying to implement your capcha to my form, which is jquary and it doesn;t seem to work. Can you help?

  27. Awesome things here. I’m very satisfied to peer your
    post. Thanks so much and I’m having a look ahead to contact
    you. Will you please drop me a e-mail?

  28. I seldom leave a response, but i did some searching
    and wound up here How to Add a Completely Custom Captcha to Any Web
    Form. And I do have a couple of questions for you if you tend not to
    mind. Could it be just me or does it look like a few of the comments appear like
    they are left by brain dead people? :-P And, if you are writing on additional online sites,
    I would like to keep up with everything fresh you have to post.
    Would you make a list of all of all your community sites like your twitter feed, Facebook
    page or linkedin profile?

  29. It works great and it is so simple. However, it also a pretty simple captcha, because the answer is the same all the time, so it’s not hard for bots to go through it. :)

Speak Your Mind