Native captcha for your form in Magento 1.7

Magento 1.7 came with a lot of changes and new features – one substantial module that was added by default is captcha. So, we would like to shed the light on this amazing feature. You know how many robots/spam bots browse the internet searching unprotected forms. Argh.. Therefore, if you have opened form for unregistered users you need to protect it with captcha to avoid spam attacks
In this article we’ll show you how to add captcha to the form you want. Let’s start.First of all, we should register our captcha module, note that captcha functionality also can be a part of your own modules – but we would like to create the separate one. This is registration file Atwix_Captcha.xml

{code type=xml}truecommunity

{/code}

The next step is creating the config.xml file under app/code/community/Atwix/Captcha/etc. Actually, we need to declare only blocks, layout and controllers. Here you go:

{code type=xml}

Atwix_Captcha_Block

standardAtwix_Captchacaptcha

atwix_captcha.xml

{/code}
After this, we need to create two blocks, which are called Captcha.php and Captcha/Zend.php. See below how to do it.
Captcha.php:

{/code}
That is captcha block, moreover – setImgHeight and setImgWidth functions allow us to set captcha size, the setFormId function defines unique identificator for captcha that’s required. Note, that such form id is not actual form id. So, make sure that

form-validate-captchaare not the same values.
You can see that we have template atwix.phtml with form HTML, and following code snippet allows us to render captcha:
{code type=xml}

getChildHtml(‘captcha’) ?>

{/code}
By the way, for rendering this page we need to declare controller TestController.php under app/code/community/Atwix/Captcha/controllers, example:
{code type=xml}
class Atwix_Captcha_TestController extends Mage_Core_Controller_Front_Action
{
public function formAction()
{
$this->loadLayout();
$this->renderLayout();
}
}
{/code}
Note: if you run Magento Enterprise edition, captcha won’t work on the CMS page because of the cache. To get captcha working with Magento’s Full page cache just add the following code after form closing tag:
{code type=xml}