Spam proof Contact Form with Zend Framework, ReCaptcha and Akismet / TypePad Antispam
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.
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.
- 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.
- RT @_lerabela: Pravin is still FM for now. #PoorDes https://t.co/cQLmjXCrFw 5 hours ago
- RT @RooiRobot: How the whole tragedy could have been avoided https://t.co/UUHBPuuZpq 5 hours ago
- RT @TomEatonSA: At what point does deliberate harm to an economy by a sitting president become treason? 5 hours ago
- RT @krisajenkins: Anakin: I'm not afraid. Yoda: You will be. (Yoda commits a function that takes 8 String arguments.) 7 hours ago
- RT @TomEatonSA: Rand drops over 2% as Zuma orders Gordhan home because trip was "unauthorized". You know what else is unauthorized? 783 act… 1 day ago