This tutorial explains how to add Invisible recaptcha register form magento 1.9. Please use the server side validation.

Invisible recaptcha register form magento
<?php class Addpeople_Validatecaptcha_Model_Customer_Customer extends Mage_Customer_Model_Customer
lớp Addpeople_Validatecaptcha_Model_Customer_Customer mở rộng Mage_Customer_Model_Customer
{ /**
/** * Validate customer attribute values.
* Xác thực giá trị thuộc tính của khách hàng. * For existing customer password + confirmation will be validated only when password is set (i.e. its change is requested)
* Đối với mật khẩu khách hàng hiện tại + xác nhận sẽ chỉ được xác thực khi mật khẩu được đặt (tức là yêu cầu thay đổi mật khẩu) *
* * @return bool
* @return bool */
*/ public function validate()
hàm công khai validate() {
{ $errors = array();
$errors = mảng(); if (!Zend_Validate::is( trim($this->getFirstname()) , ‘NotEmpty’)) {
nếu (!Zend_Validate::is( trim($this->getFirstname()) , ‘NotEmpty’)) { $errors[] = Mage::helper(‘customer’)->__(‘The first name cannot be empty.’);
$errors[] = Mage::helper(‘customer’)->__(‘Tên không được để trống.’); }
}
if (!Zend_Validate::is( trim($this->getLastname()) , ‘NotEmpty’)) {
nếu (!Zend_Validate::is( trim($this->getLastname()) , ‘NotEmpty’)) { $errors[] = Mage::helper(‘customer’)->__(‘The last name cannot be empty.’);
$errors[] = Mage::helper(‘customer’)->__(‘Họ không được để trống.’); }
}
if (!Zend_Validate::is($this->getEmail(), ‘EmailAddress’)) {
nếu (!Zend_Validate::is($this->getEmail(), ‘EmailAddress’)) { $errors[] = Mage::helper(‘customer’)->__(‘Invalid email address “%s”.’, $this->getEmail());
$errors[] = Mage::helper(‘customer’)->__(‘Địa chỉ email không hợp lệ “%s”.’, $this->getEmail()); }
}
$password = $this->getPassword();
$password = $this->getPassword(); if (!$this->getId() && !Zend_Validate::is($password , ‘NotEmpty’)) {
nếu (!$this->getId() && !Zend_Validate::is($password , ‘NotEmpty’)) { $errors[] = Mage::helper(‘customer’)->__(‘The password cannot be empty.’);
$errors[] = Mage::helper(‘customer’)->__(‘Mật khẩu không được để trống.’); }
} if (strlen($password) && !Zend_Validate::is($password, ‘StringLength’, array(self::MINIMUM_PASSWORD_LENGTH))) {
nếu (strlen($password) && !Zend_Validate::is($password, ‘StringLength’, mảng(self::MINIMUM_PASSWORD_LENGTH))) { $errors[] = Mage::helper(‘customer’)
$errors[] = Mage::helper(‘khách hàng’) ->__(‘The minimum password length is %s’, self::MINIMUM_PASSWORD_LENGTH);
->__(‘Độ dài mật khẩu tối thiểu là %s’, self::MINIMUM_PASSWORD_LENGTH); }
} if (strlen($password) && !Zend_Validate::is($password, ‘StringLength’, array(‘max’ => self::MAXIMUM_PASSWORD_LENGTH))) {
nếu (strlen($password) && !Zend_Validate::is($password, ‘StringLength’, mảng(‘max’ => self::MAXIMUM_PASSWORD_LENGTH))) { $errors[] = Mage::helper(‘customer’)
$errors[] = Mage::helper(‘khách hàng’) ->__(‘Please enter a password with at most %s characters.’, self::MAXIMUM_PASSWORD_LENGTH);
->__(‘Vui lòng nhập mật khẩu có tối đa %s ký tự.’, self::MAXIMUM_PASSWORD_LENGTH); }
} $confirmation = $this->getPasswordConfirmation();
$confirmation = $this->getPasswordConfirmation(); if ($password != $confirmation) {
nếu ($password != $confirmation) { $errors[] = Mage::helper(‘customer’)->__(‘Please make sure your passwords match.’);
$errors[] = Mage::helper(‘customer’)->__(‘Vui lòng đảm bảo mật khẩu của bạn khớp.’); }
}
$entityType = Mage::getSingleton(‘eav/config’)->getEntityType(‘customer’);
$entityType = Mage::getSingleton(‘eav/config’)->getEntityType(‘khách hàng’); $attribute = Mage::getModel(‘customer/attribute’)->loadByCode($entityType, ‘dob’);
$attribute = Mage::getModel(‘customer/attribute’)->loadByCode($entityType, ‘dob’); if ($attribute->getIsRequired() && ” == trim($this->getDob())) {
nếu ($attribute->getIsRequired() && ” == trim($this->getDob())) { $errors[] = Mage::helper(‘customer’)->__(‘The Date of Birth is required.’);
$errors[] = Mage::helper(‘customer’)->__(‘Yêu cầu nhập Ngày sinh.’); }
} $attribute = Mage::getModel(‘customer/attribute’)->loadByCode($entityType, ‘taxvat’);
$attribute = Mage::getModel(‘customer/attribute’)->loadByCode($entityType, ‘taxvat’); if ($attribute->getIsRequired() && ” == trim($this->getTaxvat())) {
nếu ($attribute->getIsRequired() && ” == trim($this->getTaxvat())) { $errors[] = Mage::helper(‘customer’)->__(‘The TAX/VAT number is required.’);
$errors[] = Mage::helper(‘customer’)->__(‘Yêu cầu nhập mã số THUẾ/VAT.’); }
} $attribute = Mage::getModel(‘customer/attribute’)->loadByCode($entityType, ‘gender’);
$attribute = Mage::getModel(‘customer/attribute’)->loadByCode($entityType, ‘gender’); if ($attribute->getIsRequired() && ” == trim($this->getGender())) {
nếu ($attribute->getIsRequired() && ” == trim($this->getGender())) { $errors[] = Mage::helper(‘customer’)->__(‘Gender is required.’);
$errors[] = Mage::helper(‘customer’)->__(‘Cần nhập giới tính.’); }
}
if( Mage::getStoreConfig(‘validate_captcha/server_side_validation/enabled’) ) {
nếu( Mage::getStoreConfig(‘validate_captcha/server_side_validation/enabled’) ) { $errors = $this->validateCaptcha($errors);
$errors = $this->validateCaptcha($errors); }
}
if (empty($errors)) {
nếu (trống ($ lỗi)) { return true;
trả về giá trị đúng; }
} return $errors;
trả về $errors; }
}
private function validateCaptcha( $errors ) {
hàm riêng tư validateCaptcha( $errors ) {
/* additional reCAPTCHA validation */
/* xác thực reCAPTCHA bổ sung */
$action = Mage::app()->getRequest()->getActionName();
$action = Mage::app()->getRequest()->getActionName(); if ( $action == ‘createpost’ ) {
nếu ( $action == ‘createpost’ ) {
$captcha = Mage::app()->getRequest()->getPost(‘g-recaptcha-response’, 1);
$captcha = Mage::app()->getRequest()->getPost(‘g-recaptcha-response’, 1); if ( $captcha == ” ) {
nếu ( $captcha == ” ) {
$errors[] = Mage::helper(‘customer’)->__(‘Please check the reCAPTCHA field to continue.’);
$errors[] = Mage::helper(‘customer’)->__(‘Vui lòng kiểm tra trường reCAPTCHA để tiếp tục.’); } else {
} khác { $params = array();
$params = mảng(); $params[‘secret’] = Mage::getStoreConfig(‘validate_captcha/server_side_validation/secret_key’);
$params[‘secret’] = Mage::getStoreConfig(‘validate_captcha/server_side_validation/secret_key’); $params[‘response’] = Mage::app()->getRequest()->getPost(‘g-recaptcha-response’, 1);
$params[‘response’] = Mage::app()->getRequest()->getPost(‘g-recaptcha-response’, 1); $params[‘remoteip’] = $_SERVER[‘REMOTE_ADDR’];
$params[‘remoteip’] = $_SERVER[‘REMOTE_ADDR’];
$params_string = http_build_query($params);
$params_string = http_build_query($params); $url = ‘https://www.google.com/recaptcha/api/siteverify?’.$params_string;
$url = ‘https://www.google.com/recaptcha/api/siteverify?’.$params_string;
$ch = curl_init();
$ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec( $ch );
$response = curl_exec( $ch );
$result = json_decode( $response, true );
$result = json_decode( $response, đúng); if ( trim( $result[‘success’] ) != true ) {
nếu (cắt( $result[‘success’] ) != true ) {
// This will be shown at the top of the registration page
// Nội dung này sẽ được hiển thị ở đầu trang đăng ký $errors[] = Mage::helper(‘customer’)->__(‘reCAPTCHA unable to verify.’);
$errors[] = Mage::helper(‘customer’)->__(‘reCAPTCHA không thể xác minh.’); }
} }
} }
}
return $errors;
trả về $errors;
}
}
}
}
Layout and config file
<models> <validatecaptcha>
<xác thực captcha> <class>Addpeople_Validatecaptcha_Model</class>
<class>Addpeople_Validatecaptcha_Model</class> <resourceModel>validatecaptcha_mysql4</resourceModel>
<resourceModel>xác thực captcha_mysql4</resourceModel> </validatecaptcha>
</validatecaptcha> <customer>
<khách hàng> <rewrite>
<viết lại> <customer>Addpeople_Validatecaptcha_Model_Customer_Customer</customer>
<customer>Addpeople_Validatecaptcha_Model_Customer_Customer</customer> </rewrite>
</viết lại>
</customer>
</models>
Layout file
<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>