Spam proof Contact Form with Zend Framework, ReCaptcha and Akismet / TypePad Antispam
Update : Download Source code here
Update : View Working Demo
Over the last few weeks I’ve blogged on using the Zend Framework’s Akismet Service to “spam proof” your generic, run of the mill contact form.
Of course, services like Akismet and TypePad Antispam only come into play after the form has been submitted, a “cure” so to speak.
What we really need is some form of robust “prevention”. Stop the spam bots right at the door. There’s nothing more irritating than getting bombarded by 10k submissions every hour on the hour – even if they’re being filtered out by your Akismet / TypePad service.
Which brings us to the now common place CAPTCHA component. Designed to stop automated spam bots in their tracks, its a pretty effective method. Ordinarily, it’s not too difficult to implement a CAPTCHA component to your form, but ReCaptcha and the Zend Framework have now made it child’s play.
ReCaptcha is an online service providing a free CAPTCHA API for you to use. What’s also really nifty is that while your users fill in the CAPTCHA form, they’re also helping develop OCR software used to digitize books and manuscripts.
As an example I’ve gone and written a basic contact form, zipped it up and made it available here. Inside the zip file is all the code necessary for a very basic and hopefully spam proof contact form.
The following Zend Framework components were used :
- Zend Loader
- Zend Config
- Zend View
- Zend Service ReCaptcha
- Zend Service Akismet
- Zend Mail
- Zend Validate
- Zend Filter
- Zend Registry
I’ve also included a simple wrapper for Zend View, since I didn’t feel that it was necessary to go the whole MVC route on this one.
The code used is pretty straightforward and easy to follow with as many comments thrown in there as I could stomach, but if you have any questions please feel free to leave a comment or send a mail.
Before you begin, you’ll need the following:
- A ReCaptcha key pair (multiple key pairs are supported by this script)
- An Akismet/TypePad Antispam API key
To use, unzip the file (it already contains the Zend Framework v1.6), edit the config.xml file and then simply customize the view scripts (view/form.php, view/success.php, view/mail.php).
Unfortunately I haven’t had much time to test the example thoroughly as it was written in a bit of a hurry (only had about an hour or so), so if you find or even fix any bugs please let me know and I’ll sort it out as soon as I get an opportunity to do so.
Update: I’ve amended 3 bugs thanks to comments from Mark. Affected files can be downloaded separately here, or you can download the full source again here. I’ve also created a working demo here.
- In view/form.php line 23 : “$this->TextArea” has been changed to “$this->Textarea”. TextArea works on my local host, but not when uploaded to my demo server.
- In view/form.php line 25-27 : Added a new paragraph containing “$this->captcha” to output the ReCaptcha element. This was not included in the zip file’s form.php for some inexplicable reason – my apologies
- In library/FlexiDev/Process.php line 122 : Logic error preventing ReCaptcha from verifying correctly. $recaptcha->verify(…) returns an object and not a boolean value – amended to $result = $recaptcha->verify(…); return $result->isValid();
One last note : the code is provided under the New BSD License, so have fun.
Share this:
Leave a Reply Cancel reply
Latest Tweets
- Building offline applications with AngularJS and PouchDB zite.to/16BpKWe 19 hours ago
- Backbone-Dropbox.Js zite.to/11LuNis > cool 19 hours ago
- Gorgeous day outside. What winter? @ George Golf Club instagram.com/p/aqNmgmtZ7b/ 1 day ago
- 76, 92 (yikes), 78. I would've been in contention at Merion with those scores :P 1 day ago
- If the latest Star Trek is anything to go by I cannot wait to see what JJ does with Star Wars. 3 days ago
Archives
Recent Posts
- Yet another list of ‘extremely useful’ jQuery plugins
- Advanced radio button grouping with jQuery
- Mobile App Development – Snake Guide for South Africa
- Howto: Create custom thumbnails from Flutter image fields
- Simplifying Flutter duplicate groups and fields
- Circular Image Slide with jQuery
- 6 jQuery snippets you can use to manipulate select inputs
- How to validate Date of Birth with jQuery
- Still alive and kicking
- jQuery plugin : jqPageFlow – scrolling pagination made easy
Top Posts
- 6 jQuery snippets you can use to manipulate select inputs
- How to use jQuery to select a radio button from parent element
- Circular Image Slide with jQuery
- Javascript / Jquery Bookmark script
- How to validate Date of Birth with jQuery
- Jquery UI Layout Manager Plugin
- Spam proof Contact Form with Zend Framework, ReCaptcha and Akismet / TypePad Antispam
- Howto: Create custom thumbnails from Flutter image fields
- jQuery plugin : jqPageFlow - scrolling pagination made easy
- Mobile App Development - Snake Guide for South Africa

Hummm, nice script, but I can’t get it to work. I think the name of the element “formTextArea” isn’t valid in Zend, because I keep on getting error messages relating to that. Any ideas?
Thanks,
Mark
Hi Mark,
Thanks for the headsup – unfortunately I didn’t have enough time to test it completely so I’ll definitely look into it and get back to you.
Hi again Mark,
Had a look – formTextArea() is definitely a valid method : http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial : Also tested again on my local system and all seems to work as expected.
Do you not perhaps have an older version of Zend Framework running ?
If you still can’t come right mail the error msg to me : calisza at gmail dot com : and we’ll see what we can organize.
Works great!! My comment spam has disappeared! But, I’d like to be able to change the Recaptcha to the “clean” style. So, instead of being the red color, it turns white. The javascript code to do this is supposed to be:
var RecaptchaOptions = {
theme: ‘clean’
};
I’ve tried inserting this in a few different files but it doesn’t work. Do you know how I can switch to the “clean” theme?
http://wiki.recaptcha.net/index.php/Theme
$recaptcha = new Zend_Service_ReCaptcha(…);
$recaptcha->setOption(‘theme’, ‘clean’);
or:
http://www.nabble.com/setOptions-in-reCaptcha-form-element-td21075556.html
I just tried this and whenever you get everything else typed in, but you don’t type anything in the reCAPTCHA or you get it wrong, you get a page of gibberish.
Thanks – looks like the reCAPTCHA API has changed somewhat since I wrote this script. Will have a look at it when I get a moment.