2

How can I implement a captcha in my custom form?

Actually I have installed a forum module where I need to implement a captcha for preventing spam.

<form action="<?php echo $this->getFormAction(); ?>" id="messageForm" method="post">
  <input name="title" id="title" title="<?php echo $this->__('Title') ?>" value="<?php echo $this->__('RE : %s',$this->htmlEscape($this->topic->getTitle())) ?>"  />
  <textarea name="content" id="content" title="<?php echo $this->__('Message') ?>" value="" class="required-entry"></textarea>
  <button class="button" type="submit"><span><?php echo $this->__('Submit') ?></span></button>
  <script type="text/javascript">
    var messageForm = new VarienForm('messageForm', true);
  </script>
  • This question is fairly broad. Are you looking for a programmatic solution? Please be more specific. – pspahn Feb 27 '13 at 7:27
  • I am not looking for detailed answer containing all code. Just looking the best approach for implementing Captcha in MAGENTO. – Anant Feb 27 '13 at 8:02
  • Please update your question with information on how you created your custom form? – user487772 Feb 27 '13 at 8:13
  • Captchas are not worth to implement. Did you try to add a new text field, style it with display: none and check wether something is in it? If yes, it is spam. Captchas are extremly annoying and easy to solve, often by computers, if not by paid humans. And in magento 1.7 there is the option to have captchas in the customer registration. – Fabian Blechschmidt Feb 27 '13 at 8:28
  • As others have commented you need to provide more information on what elements you want to protect with a Captcha. Eg. Contact form, checkout etc – Ben Lessani - Sonassi Feb 27 '13 at 9:46
1

If you (your client) must go with CAPTCHA, why not backport the CE 1.7.0.2 Mage_Captcha module [link]?

I believe you could user your form view's full action name layout update handle and an <update /> directive to reuse some of the module's layout updates for your form. You might want to add an appropriate configuration xpath to set the title for your usage.

  • Back port? How about upgrade! :) – kalenjordan Mar 2 '13 at 4:30
2

Visit this link for details, it solved my problem; hope it helps someone. http://www.magentocommerce.com/boards/view./viewthread/298363/

If you are using "RWD" theme copy captcha.xml from base/default/layout to your_rwd_theme/default/layout. This is necessary as rwd themes design have this shortfall.

Quick outline how to add Magento builtin captcha mechanism to Contact Us default page.

1) To add Contact Us form to Admin/Config interface you could a) find captcha/forms element in core_config table and add “contact_form” form name to the list b) modify config.xml in /app/code/core/Mage/Captcha/etc in - <frontend> - <areas> add something like

<contact_form>
    <label>Contact Us</label> 
</contact_form>

and in - <always_for>

<contact_form>1</contact_form>

Also there’s a miss for translation in the config file, so you should add this to the <frontend> section:

<translate>
 <modules>
 <Mage_Captcha>
 <files>
  <default>Mage_Captcha.csv</default> 
  </files>
  </Mage_Captcha>
  </modules>
 </translate>

Refresh cache. Login to Admin, and make sure Contact Us form is listed in CAPTCHA configuration

2) Modify captcha layout xml file in frontend/base/default (or your theme) add this section

<contacts_index_index>
 <reference name="contactForm">
 <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_form</formId> 
  </action>
 <action method="setImgWidth">
  <width>230</width> 
  </action>
<action method="setImgHeight">
  <width>50</width> 
  </action>
  </block>
  </block>
  </reference>
  </contacts_index_index>

Refresh cache

3) Modify form code in /frontend/base/default/template/contacts/form.phtml to add placeholder to render captcha layout Add this line of code right before </ul>

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

4) FInal part - how to\\"trap\\" the post from the Contact Us form and check the captcha Mage uses Events and Observer to achieve this, and it works very well for the forms/events they have implemented this feature. I was not able to find events specific for Contact Us form, so, IMHO the most obvious solution is to override indexController in code/core/Mage/Contacts/controllers and in the postAction method, inside try block add the following code:

$formId = 'contact_form';
           $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;
            }
           }

and add this \\"utility method to the controller class:

protected function _getCaptchaString($request, $formId)
    {
        $captchaParams = $request->getPost(Mage_Captcha_Helper_Data::INPUT_NAME_FIELD_VALUE);
        return $captchaParams[$formId];
    }

5) Refresh cache, and give it a try

  • It would have been better if you have summarized here, whatever useful information have you got from that link :) – Anant Nov 28 '14 at 3:58
  • 1
    add brief things of content in the provided link and then add link a your reference and BTW welcome to MSE :) – dh47 Nov 28 '14 at 6:40
-1

How to add new form to CAPTCHA settings?

Create new module and add lines to config.xml

How to show CAPTCHA in contacts form?

Find your form template and insert code.

After that find layout xml for your page, for example mine was contacts.xml

How to check CAPTCHA?

Create Observer for your module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.