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.
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.
I really like this post.
Could it be implemented on my form page here:
http://www.topspeedracer.com/topspeed-fan-club.html
Thanks!
Yeah, it should work just fine on your signup form.
Hey
will u upload as simple as captcha an upload file function form.
Thanks
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!!!
Thanks and you’re welcome! That’s great, I’m glad you found it so helpful. 😀
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)
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
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 😮
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…
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?
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.
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
I also have a .cgi form. Will it work?
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?
Steve, I wasn’t getting any e-mails either. Then I uploaded the form to my webserver and it worked.
If you are testing this from localhost (MAMP, WAMP etc) try putting it online.
Hmm, not sure why it wouldn’t be working locally, but I’m glad it’s working for ya 🙂
Daniel, you helped me A LOT with this straightforward approach.
No tinky-winky-bs on your form and captcha tutorial.
Thanks.
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 [email protected]
Thank you
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
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!
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?
asd
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
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.
Would it be possible to use random sums to solve? If necessary from a source table (yup I’m a programmer used to SQL)with a list of sums combined with the outcome?
I have this allergy against hard coded ‘variables’ 🙂
I think this iss among the most significant info for me.
And i am glad reading your article. But should remark on some general things, The site style iss perfect,
the articles is really great : D. Good job, cheers
I am having trouble with the captcha verification. If anything with a 5 is submitted then it passes not just a 5 (so 5444539 passes and so does 5abeed). Any thoughts on how to fix this?
Hello,
I understand where to put the info in step one, but am unclear where the remaining code goes. I’m adding this to an already existing php form that also has a response email I like and don’t want to interfere with.
@Pete - no need for the regular expression, simply use
if (!$captcha == “5”)
Learn More on CDS Views in ABAP here
Mr. Anubhav from https://www.anubhavtrainings.com/abap-on-hana-training furnishing trainings to
distinctive IT corporates from final 11+ Decades on diverse SAP Technologies plus some JavaScript Technologies.He’s listed here
to produce lectures on the net so that everybody can obtain my understanding throughout the world.The real key
energy that he will make intricate points uncomplicated And that i will likely be pleased if men and women like my films
and ask issues to me pertaining to technologies.
his experience is totally on following systems. SAP ABAP, OOPS
ABAP, CDS views, Fiori, OData, Gateway, Java Script, sap cloud platform, abap on hana,
s4 hana, hana xs, nativa hana development, ui5, fiori, sap fiori launchpad, security
of fiori applications and many a lot more.