如何在客户注册表格中获得验证码?


8

最近,我们在网站上遇到了垃圾邮件发送者的麻烦,我们希望使用验证码功能来解决此问题。

我在系统->配置->客户->客户配置-> CAPTCHA中启用了验证码功能

现在,我们有一个主题,其中不存在验证码布局/功能。

因此,我尝试将其纳入主题并从基础复制了代码。这两个captcha.xmlcaptcha/zend.phtml

不幸的是,这没有用,所以我只想尝试将reCAPTCHA手动添加到register.phtml

但是无论是在主题目录还是在基本目录中,编辑customer/register.phtml或都persistent/customer/register.phtml没有改变。

所以我的问题是,我该怎么做?另外,我也不知道你们需要什么信息才能帮助我。因此,要求任何东西。


尝试编辑在/app/design/frontend/base/default/template/opc/customer/form/register.phtml
Păduraru利维乌·罗伯特·

Answers:


0

StudioForty9有一个很棒的免费扩展程序,我在Magento 1.9上一直在使用它-您可以选择验证码显示在网站的哪些区域,到目前为止,对我来说,它是完美无缺的。

Amasty还为M1提供了一个免费的不可见的Captcha扩展名,但我尚未尝试过。


0

此脚本用于进行验证,例如magento的默认验证。请使用它。

<form name="freeeventForm" id="freeeventForm">
    <div id="RecaptchaField"></div>
    <input type="hidden" class="validate-reCAPTCHA">
</form>
    <script src="https://www.google.com/recaptcha/api.js?onload=CaptchaCallback&render=explicit" async defer></script>
    <script type="text/javascript">
        //< ![CDATA[
            var CaptchaCallback = function() {  
            grecaptcha.render('RecaptchaField', {'sitekey' : '6LeuiDwUAAAAALByt-xxxxxxxxxxx-xUsZHFkeEP'});
        };
        var customForm = new VarienForm('freeeventForm');
        Validation.add('validate-reCAPTCHA','reCAPTCHA is mandatory',function(){
            var response = grecaptcha.getResponse();
            if (response.length === 0) {
                    return false;
            }
            return true;
    });
        //]]>
    </script>

0

请使用以下代码进行服务器端验证。

<models> <validatecaptcha> <class>Addpeople_Validatecaptcha_Model</class> <resourceModel>validatecaptcha_mysql4</resourceModel> </validatecaptcha> <customer> <rewrite> <customer>Addpeople_Validatecaptcha_Model_Customer_Customer</customer> </rewrite> </customer> </models>

改写注册类

<?php class Addpeople_Validatecaptcha_Model_Customer_Customer extends Mage_Customer_Model_Customer {
/**
 * Validate customer attribute values.
 * For existing customer password + confirmation will be validated only when password is set (i.e. its change is requested)
 *
 * @return bool
 */
public function validate()
{
    $errors = array();
    if (!Zend_Validate::is( trim($this->getFirstname()) , 'NotEmpty')) {
        $errors[] = Mage::helper('customer')->__('The first name cannot be empty.');
    }

    if (!Zend_Validate::is( trim($this->getLastname()) , 'NotEmpty')) {
        $errors[] = Mage::helper('customer')->__('The last name cannot be empty.');
    }

    if (!Zend_Validate::is($this->getEmail(), 'EmailAddress')) {
        $errors[] = Mage::helper('customer')->__('Invalid email address "%s".', $this->getEmail());
    }

    $password = $this->getPassword();
    if (!$this->getId() && !Zend_Validate::is($password , 'NotEmpty')) {
        $errors[] = Mage::helper('customer')->__('The password cannot be empty.');
    }
    if (strlen($password) && !Zend_Validate::is($password, 'StringLength', array(self::MINIMUM_PASSWORD_LENGTH))) {
        $errors[] = Mage::helper('customer')
            ->__('The minimum password length is %s', self::MINIMUM_PASSWORD_LENGTH);
    }
    if (strlen($password) && !Zend_Validate::is($password, 'StringLength', array('max' => self::MAXIMUM_PASSWORD_LENGTH))) {
        $errors[] = Mage::helper('customer')
            ->__('Please enter a password with at most %s characters.', self::MAXIMUM_PASSWORD_LENGTH);
    }
    $confirmation = $this->getPasswordConfirmation();
    if ($password != $confirmation) {
        $errors[] = Mage::helper('customer')->__('Please make sure your passwords match.');
    }

    $entityType = Mage::getSingleton('eav/config')->getEntityType('customer');
    $attribute = Mage::getModel('customer/attribute')->loadByCode($entityType, 'dob');
    if ($attribute->getIsRequired() && '' == trim($this->getDob())) {
        $errors[] = Mage::helper('customer')->__('The Date of Birth is required.');
    }
    $attribute = Mage::getModel('customer/attribute')->loadByCode($entityType, 'taxvat');
    if ($attribute->getIsRequired() && '' == trim($this->getTaxvat())) {
        $errors[] = Mage::helper('customer')->__('The TAX/VAT number is required.');
    }
    $attribute = Mage::getModel('customer/attribute')->loadByCode($entityType, 'gender');
    if ($attribute->getIsRequired() && '' == trim($this->getGender())) {
        $errors[] = Mage::helper('customer')->__('Gender is required.');
    }

    if( Mage::getStoreConfig('validate_captcha/server_side_validation/enabled') ) {
        $errors = $this->validateCaptcha($errors);
    }

    if (empty($errors)) {
        return true;
    }
    return $errors;
}

private function validateCaptcha( $errors ) {

    /* additional reCAPTCHA validation */

    $action = Mage::app()->getRequest()->getActionName();
    if ( $action == 'createpost' ) {

        $captcha = Mage::app()->getRequest()->getPost('g-recaptcha-response', 1);
        if ( $captcha == '' ) {

            $errors[] = Mage::helper('customer')->__('Please check the reCAPTCHA field to continue.');
        } else {
            $params = array();
            $params['secret'] =  Mage::getStoreConfig('validate_captcha/server_side_validation/secret_key');
            $params['response'] = Mage::app()->getRequest()->getPost('g-recaptcha-response', 1);
            $params['remoteip'] = $_SERVER['REMOTE_ADDR'];

            $params_string = http_build_query($params);
            $url = 'https://www.google.com/recaptcha/api/siteverify?'.$params_string;

            $ch = curl_init();
            curl_setopt( $ch, CURLOPT_URL, $url );
            curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
            $response = curl_exec( $ch );

            $result = json_decode( $response, true );
            if ( trim( $result['success'] ) != true ) {

                // This will be shown at the top of the registration page
                $errors[] = Mage::helper('customer')->__('reCAPTCHA unable to verify.');
            }
        }
    }

    return $errors;

}

}

布局xml文件

<customer_account_create> <reference name="form.additional.info"> <block type="core/template" name="captcha_js" template="validatecaptcha/captchajs.phtml" /> <block type="core/template" name="validate_captcha" template="validatecaptcha/index.phtml"/> </reference> </customer_account_create>

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.