Magento native captcha for contact form

本文介绍如何在Magento中配置CAPTCHA,包括添加新的表单到CAPTCHA设置、在联系表单中显示CAPTCHA以及如何检查CAPTCHA的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Native CAPTCHA settings

First of all find Customer Configuration and open CAPTCHA settings

How to add new form to CAPTCHA settings?

Create new module and add this lines to config.xml:

<?xml version="1.0"?>
<config>
  ...
  <global>
    <events>
      <controller_action_predispatch_routename_controller_method>
        <observers>
          <module>
            <class>module/observer</class>
            <method>checkCaptcha</method>
          </module>
        </observers>
      </controller_action_predispatch_routename_controller_method>
    </events>
  </global>
  <default>
    <captcha>
      <frontend>
        <areas>
          <contact_form>
            <label>Contact Form</label>
          </contact_form>
        </areas>
      </frontend>
    </captcha>
    <customer>
      <captcha>
        <always_for>
          <contact_form>1</contact_form>
        </always_for>
      </captcha>
    </customer>
  </default>
</config>

 You might found here module/observer. It is explained further.

How to show CAPTCHA in contacts form?

Find your form template and insert:

<form action="<?php echo $this->getFormAction(); ?>" id="contactForm" method="post" class="form">
...
<?php echo $this->getChildHtml('captcha'); ?>
...
</form>

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

<layout version="0.1.0">
    <contacts_index_index translate="label">
        <label>Contact Us Form</label>
        <reference name="root">
            <action method="setTemplate"><template>page/3columns.phtml</template></action>
            <action method="setHeaderTitle" translate="title" module="contacts"><title>Contact Us</title></action>
        </reference>
        <reference name="content">
            <block type="core/template" name="contactForm" template="contacts/form.phtml">
                <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>
</layout>

 

How to check CAPTCHA?

Create Observer for your module:

<?php
class Your_Module_Model_Observer
{
    /**
     * Get Captcha String
     *
     * @param Varien_Object $request
     * @param string $formId
     * @return string
     */
    protected function _getCaptchaString($request, $formId)
    {
        $captchaParams = $request->getPost(Mage_Captcha_Helper_Data::INPUT_NAME_FIELD_VALUE);
        return $captchaParams[$formId];
    }
 
    /**
     * Break the execution in case of incorrect CAPTCHA
     *
     * @param Varien_Event_Observer $observer
     * @return Your_Module_Model_Observer
     */
    public function checkCaptcha($observer)
    {
        $formId = 'contact_form';
        $captchaModel = Mage::helper('captcha')->getCaptcha($formId);
        if ($captchaModel->isRequired()) {
            $controller = $observer->getControllerAction();
            if (!$captchaModel->isCorrect($this->_getCaptchaString($controller->getRequest(), $formId))) {
                Mage::getSingleton('customer/session')->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
                $controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
                Mage::getSingleton('customer/session')->setCustomerFormData($controller->getRequest()->getPost());
                $controller->getResponse()->setRedirect(Mage::getUrl('*/*/index'));
            }
        }
 
        return $this;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值