Home > PHP > Spam proof Contact Form with Zend Framework, ReCaptcha and Akismet / TypePad Antispam

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:

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.

  1. 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.
  2. 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
  3. 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.

  1. December 11, 2008 at 12:05 am

    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?



    • calisza
      December 11, 2008 at 12:02 pm

      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.

    • calisza
      December 11, 2008 at 7:22 pm

      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.

  2. Billy
    July 22, 2009 at 1:39 am

    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?

  3. September 28, 2009 at 2:53 am


    $recaptcha = new Zend_Service_ReCaptcha(…);
    $recaptcha->setOption(‘theme’, ‘clean’);


  4. January 25, 2010 at 2:16 am

    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.

    • January 26, 2010 at 3:04 pm

      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.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: