How To Create Custom Captcha For Your Magento ”Contact Us” Page How To Create Custom Captcha For Your Magento ”Contact Us” Page

At some time, you might have created a custom form in Magento for your client’s need. But later, your client informed you he is getting spam from that form. A practical solution for securing Magento contact form from spam is to use “Captcha” in your form. Captcha will prevent the bots from using your form.  

 

For using core captcha functionality in your “Contact Us” form, you need to develop a module in your Magento directory. In this article, I’m going to show you how to create a custom captcha for your Magento contact us form.

 

Step 1: First of all, you need to create a module config file. “Cloudways” is my name space and “Captcha” is my module name. Create Cloudways_Captcha.xml in app/etc/modules/ and use this code in your Cloudways_Captcha.xml file.

 

<?xml version=”1.0″?>

<config>

<modules>

<Cloudways_Captcha>

<active>true</active>

<codePool>local</codePool>

<depends>

<Mage_Captcha/>

</depends>

</Cloudways_Captcha>

</modules>

</config>

 

Step 2: after creating config file, Now create module your module config.xml. Create this file in app/code/local/Cloudways/Captcha/etc/config.xml.

 

<captcha>

<rewrite>

<zend>Cloudways_Captcha_Model_Zend</zend>

</rewrite>

</captcha>

</models>

</global>

<default>

<captcha  translate=”label”>

<frontend>

<areas>

<contact_us>

<label>Contact us Form</label>

</contact_us>

</areas>

</frontend>

</captcha>

<customer>

<captcha>

<always_for>

<contact_us>1</contact_us>

</always_for>

</captcha>

</customer>

</default>

<frontend>

<routers>

<contacts>

<args>

<modules>

<Cloudways_captcha before=”Mage_Contacts”>Cloudways_Captcha</Cloudways_captcha>

</modules>

</args>

</contacts>

</routers>

</frontend>

</config>

 

Step 3: Now, create Zend.php to make captcha appear to customer after customer logs in. To display the captcha after customer login, you must override Mage_Captcha_Model_Zend file _isUserAuth() function. Create Zend.php file in app/code/local/Cloudways/Captcha/Model/Zend.php.

 

<?php

class Cloudways_Captcha_Model_Zend extends Mage_Captcha_Model_Zend

{

protected function _isUserAuth()

{

//die(‘function called’);

/* return Mage::app()->getStore()->isAdmin()

? Mage::getSingleton(‘admin/session’)->isLoggedIn()

: Mage::getSingleton(‘customer/session’)->isLoggedIn();*/

}

}

 

Step 4: Now, create frontend layout. You need to edit Magento contacts.xml file. This is located in app/design/frontend/rwd/default/layout/contacts.xml add captcha block under “<reference name=”content”>

<block type=”core/template” name=”contactForm” template=”contacts/form.phtml”/>”

 

<block type=”core/text_list” name=”form.additional.info”>

<block type=”captcha/captcha” name=”captcha”>

<reference name=”head”>

<action method=”addJs”><file>mage/captcha.js</file></action>

</reference>

<action method=”setFormId”><formId>contact_us</formId></action>

<action method=”setImgWidth”><width>230</width></action>

<action method=”setImgHeight”><width>50</width></action>

</block>

</block>

</block>

 

Step 5: After that, you need to call block in frontend form.phtml file. This file is located in  app/design/frontend/rwd/default/template/contacts/form.phtml.

 

<?php echo $this->getChildHtml(‘form.additional.info’); ?>

put this code after </ul> tag.

 

Step 6: After this, you need to create contact us controller file in app/code/local/Cloudways/Captcha/controllers/IndexController.php

 

<?php

require_once(Mage::getModuleDir(‘controllers’,’Mage_Contacts’).DS.’IndexController.php’);

class Cloudways_Captcha_IndexController extends Mage_Contacts_IndexController

{

public function postAction()

{

$post = $this->getRequest()->getPost();

if ( $post ) {

$translate = Mage::getSingleton(‘core/translate’);

/* @var $translate Mage_Core_Model_Translate */

$translate->setTranslateInline(false);

try {

$postObject = new Varien_Object();

$postObject->setData($post);

 

$error = false;

 

if (!Zend_Validate::is(trim($post[‘name’]) , ‘NotEmpty’)) {

$error = true;

}

if (!Zend_Validate::is(trim($post[‘comment’]) , ‘NotEmpty’)) {

$error = true;

}

if (!Zend_Validate::is(trim($post[’email’]), ‘EmailAddress’)) {

$error = true;

}

if (Zend_Validate::is(trim($post[‘hideit’]), ‘NotEmpty’)) {

$error = true;

}

$formId =’contact_us’;

$captchaModel = Mage::helper(‘captcha’)->getCaptcha($formId);

if ($captchaModel->isRequired()) {

if (!$captchaModel->isCorrect($this->_getCaptchaString($this->getRequest(), $formId))) {

Mage::getSingleton(‘customer/session’)->addError(Mage::helper(‘captcha’)->__(‘Incorrect CAPTCHA.’));

$this->setFlag(”, Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);

Mage::getSingleton(‘customer/session’)->setCustomerFormData($this->getRequest()->getPost());

$this->getResponse()->setRedirect(Mage::getUrl(‘*/*/’));

return;

}

}

 

if ($error) {

throw new Exception();

}

$mailTemplate = Mage::getModel(‘core/email_template’);

/* @var $mailTemplate Mage_Core_Model_Email_Template */

$mailTemplate->setDesignConfig(array(‘area’ => ‘frontend’))

->setReplyTo($post[’email’])

->sendTransactional(

Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),

Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),

Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),

null,

array(‘data’ => $postObject)

);

if (!$mailTemplate->getSentSuccess()) {

throw new Exception();

}

$translate->setTranslateInline(true);

Mage::getSingleton(‘customer/session’)->addSuccess(Mage::helper(‘contacts’)->__(‘Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.’));

$this->_redirect(‘*/*/’);

return;

} catch (Exception $e) {

$translate->setTranslateInline(true);

Mage::getSingleton(‘customer/session’)->addError(Mage::helper(‘contacts’)->__(‘Unable to submit your request. Please, try again later’));

$this->_redirect(‘*/*/’);

return;

}

} else {

$this->_redirect(‘*/*/’);

}

}

 

protected function _getCaptchaString($request, $formId)

{

$captchaParams = $request->getPost(Mage_Captcha_Helper_Data::INPUT_NAME_FIELD_VALUE);

return $captchaParams[$formId];

}

}

 

Your captcha module is ready. Now, go to your Magento admin and system > configuration > customer configuration > Captcha and enable captcha if it is disabled. You can see “Contact us Form” in multi select option, select Contact us Form option and after this click on save configuration button. Clear cache and you will see the captcha on the front-end.

 

Article written by Wajid Hussain

Wajid Hussain is a Magento Community Manager at Cloudways. He is an experienced in Magento Front-end and PHP development. He loves to play football & basketball. You can find him on Twitter or Linkedin.

No comments so far.

Be first to leave comment below.

Your email address will not be published. Required fields are marked *