`
kirenenko04
  • 浏览: 147229 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

Magento native captcha for contact form

 
阅读更多

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;
    }
}

 

分享到:
评论

相关推荐

    smaily-magento-extension:Magento 2.0+的Smaily扩展

    Magento内置的CAPTCHA和Google reCAPTCHA支持。 Magento产品RSS提要 生成具有50种最新产品的默认RSS提要,以轻松导入到Smaily模板中; 可以根据产品类别自定义生成的RSS提要的选项; 选择将生成的RSS提要产品的...

    magento-react-native:适用于Magento 2.x的React Native移动应用

    适用于Magento 2的开源React Native移动应用 Magento React Native是适用于您的Magento 2网站的功能齐全的电子商务应用程序。 它使用来分发目录,产品,将产品添加到购物车,下订单等等。 :megaphone: 宣告这个项目...

    magento数据结构分析

    magento数据结构分析 magento数据字典

    Magento 2 Beginners Guide

    He is an active member of the community and has been recognized as a Magento Master for 2017. He is the Chief Operating Officer of Wagento, a full-service Magento Solution Partner with offices in ...

    magento static block

    New GUI added 10 predefined block positions on each page Ability to place any block in any position by easily ... Meets Magento programming practices for most versions and customizations compatibility

    Magento-SMTP-Email

    magento的SMTP插件magento的SMTP插件magento的SMTP插件

    magik shoes magento 模板, magento 1.7 模板

    magik shoes magento 模板, magento 1.7 模板

    Ajax-magento2-ajax-cart-quick.zip

    Ajax-magento2-ajax-cart-quick.zip,magento 2 ajax购物车扩展插件提供舒适的购物体验。客户可以很容易地选择可配置的选项并在弹出窗口中编辑项目,而不会浪费重新加载页面的时间。,ajax代表异步javascript和xml。它...

    用于 Magento 2.x 的 React Native 移动应用程序

    适用于 Magento 2 的开源 React Native 移动应用程序 Magento React Native 是适用于您的 Magento 2 网站的功能齐全的电子商务应用程序。 它使用 Magento 2 REST API 来显示目录、产品、将产品添加到购物车、下订单...

    Magento深入理解Magento

    深入理解Magento 由精东博客下载下来制作成pdf

    Magento插件开发手册 Magento Extension Developers Guide

    Magento插件开发手册 Magento Extension Developers Guide

    Athlete Magento免费模板

    Athlete Magento主题,兼容Magento 1.9.x, 1.8.x, 1.7.x ,特点:完全自适应(响应式布局),自定义子主题,无限颜色,完全自定义,一键安装演示,小工具化横幅系统,颜色选择,MEGA菜单,多商店支持,自定义产品...

    magento2 developers cookbook

    magento2 developers cookbook, magento 开发手册,magento教程

    Magento商城_目录结构说明

    Magento Magento商城 Magento目录结构

    magento入门学习资料

    主要包括的内容有:magento的常用设置、Magento模板制作教程、Magento商城_目录结构说明、Magento中文教程实用文档、常用的xml的文件的作用以及自定义模块的应用

    Magento 2 Cookbook

    Magento 2 is installable only via the command-line interface for an experienced webmaster. Chapter 2, Magento 2 System Tools, explains how to install Magento 2 via the command shell. Magento released ...

    magento快速复制网站_magento_magento快速复制站_

    快速在服务器上复制为另一个站,版本用的是magento,php语言

    magento_react_native:age法师推车

    MageCart:Magento 2 React Native电子商务应用程序 MageCart是适用于Magento 2.1及更高版本的电子商务应用程序。 它使用来显示目录,产品,将产品添加到购物车并让您下订单。 :camera: 屏幕截图 :mobile_phone...

    Magento模块开发教程.pdf

    Magento 的配置系统就像是 Magento 的心脏,支撑着 Magento 的运行。这套 配置系统掌管着几乎所有“module/model/class /template/etc”。它把整个 Magento 系统抽象出来,用一个配置文件来描述。这里的“配置文件”...

    [Magento] Magento 扩展开发入门教程 (英文版)

    [Packt Publishing] Magento 扩展开发入门教程 (英文版) [Packt Publishing] Getting Started with Magento Extension Development (E-Book) ☆ 图书概要:☆ Understand Magento extensions, and build your own...

Global site tag (gtag.js) - Google Analytics