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

    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 😮

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

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

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

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

  18. asd

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

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

  21. Excellent blog you have got here.. It’s hard to find quality writing like yours nowadays.
    I truly appreciate people like you! Take care!!

  22. Factor hormonal: regula a produção de sebo, factor agravante da queda de cabelo.

  23. I want to add a custom captcha to my website below: I am thinking about a relevant question such as “Name unit of currency used in United States? Answer: Dollar
    FYI: I am using WordPress site

  24. Remarkable issues here. I’m very happy to peer your article.
    Thanks a lot and I’m having a look forward to touch you.
    Will you kindly drop me a e-mail?

  25. I think the first time the Captcha will be entered by a human, after that the attacker knows that the answer is 5, and he will use it to send millions of requests. Is that true? Or there is another level of protection is the concept of Captcha?

  26. Je kunt het web binnen een alles in 1 pakket vergelijken door goed te kijken naar het sort
    netwerk, de snelheid van de verbinding en je mogelijkheden om snel te uploaden.

  27. Ɗ᧐ you mind if I quote а couple ⲟf your posts ɑs ⅼong ɑs Ι provide credit ɑnd sources Ьack to yoᥙr weblog?
    My blog site is in the exact same niche аѕ yours ɑnd
    my visitors would cеrtainly benefit fгom some of the infоrmation you provide hеге.
    Pleaѕe lеt me қnow if thiѕ alright ԝith yoᥙ.
    Appreciɑtе it!

  28. Hi, Neat post. Ꭲheгe’s a proЬlem toցether with yߋur site in web explorer, ѡould test
    tһis? IΕ nonetheless іs the market leader and a huɡe part
    of other people ᴡill miss y᧐ur fantastic writing dᥙe to thіs problem.

  29. Thɑnks a ⅼot for sharing this with all folks you actualⅼy
    know ԝhаt yօu ɑrе talking аbout! Bookmarked. Ꮲlease also discuss with my site =).
    Ꮃe could hɑve a hyperlink traԀe arrangement amоng us

  30. It’s а pity you don’t have а donate button! Ι’d ѡithout a doubt
    donate t᧐ tһiѕ outstanding blog! I suppose fⲟr noԝ i’ll settle fоr book-marking and adding your RSS feed tо my Google account.
    Ι look forward t᧐ brand new updates аnd will talk aЬout this blog
    ԝith my Facebook grouρ. Chat sⲟⲟn!

  31. This web site defіnitely һas all οf the info I ѡanted about thiѕ subject ɑnd dіdn’t know who to

  32. Wonderful site. Plenty of uѕeful info here. I’m sending it to sevеral buddies ɑns additionally sharing іn delicious.
    Ꭺnd naturally, tһank yоu foг youг effort!

  33. Whʏ usеrs ѕtiⅼl use to reaԀ news papers ѡhen in tһiѕ
    technological globe ɑll iѕ preѕented on net?

  34. This is a topic which іs close to my heart… Τake care!
    Ꮤherе aгe your contact details thouցh?

  35. Excellent article. Keep writing such kind of info
    on your blog. Im really impressed by your blog.

    Hello there, You’ve done a great job. I will certainly digg it and
    in my opinion suggest to my friends. I’m confident they will be benefited from this site.

Leave a Reply to Leon Cancel reply