Solusi JavaScript yang diterima di atas jelas BUKAN cara untuk menurut saya. Bot apa pun yang tidak menggunakan JS (yang sebagian besar darinya) hanya akan mem-bypass validasi Anda dan Anda akan mendapatkan semua spam yang Anda coba blokir. Selalu selalu memvalidasi di server. Validasi JS hanyalah langkah pertama UX.
Bagaimanapun, ada beberapa solusi, tapi inilah yang bekerja untuk saya di Magento 1.9 setelah banyak penelitian. Ini awalnya dibangun di atas jawaban Mike di atas, tetapi swap file_get_contents untuk cURL karena fungsi sebelumnya biasanya akan memberi Anda kesalahan http wrapper tergantung pada konfigurasi server Anda.
Buat modul Anda sendiri dengan Membuat folder / aplikasi / kode / lokal / YourVendorName / ValidateCaptcha /
Di folder ValidateCaptcha baru Anda, tambahkan folder Model dengan file Customer.php. Ini akan digunakan untuk mengganti file inti Customer.php yang disediakan oleh Magento.
Salin & Tempel kode ini:
<?php
class YourVendorName_ValidateCaptcha_Model_Customer extends Mage_Customer_Model_Customer {
/**
* Validate customer attribute values.
*
* @return bool
*/
public function validate()
{
// This section is from the core file
$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(6))) {
$errors[] = Mage::helper('customer')->__('The minimum password length is %s', 6);
}
$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.');
}
// additional reCAPTCHA validation
// this should actually be in it's own function, but I've added
// it here for simplicity
// Magento uses this method for a few different requests, so make
// sure it's limited only to the 'createpost' action
$action = Mage::app()->getRequest()->getActionName();
if ( $action == 'createpost' ) { // restrict to the registration page only
$captcha = Mage::app()->getRequest()->getPost('g-recaptcha-response', 1);
if ( $captcha == '' ) {
// if the field is empty, add an error which will be
// displayed at the top of the page
$errors[] = Mage::helper('customer')->__('Please check the reCAPTCHA field to continue.');
} else {
$secret = 'your-secret-key-goes-here';
$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . $secret . '&response=' . $captcha . '&remoteip=' . $_SERVER["REMOTE_ADDR"];
$ch = curl_init();
// if you're testing this locally, you'll likely need to
// add your own CURLOPT_CAINFO parameter or you'll get
// SSL errors
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 ) {
// Add reCAPTCHA error
// This will be shown at the top of the registration page
$errors[] = Mage::helper('customer')->__('reCAPTCHA unable to verify.');
}
}
}
// now return the errors with your reCAPTCHA validation as well
if (empty($errors)) {
return true;
}
return $errors;
}
}
Sekarang tambahkan folder dll ke modul Anda dan buat config.xml dengan yang berikut ini:
<?xml version="1.0"?>
<config>
<modules>
<YourVendorName_ValidateCaptcha>
<version>1.0</version>
</YourVendorName_ValidateCaptcha>
</modules>
<global>
<models>
<customer>
<rewrite>
<customer>YourVendorName_ValidateCaptcha_Model_Customer</customer>
</rewrite>
</customer>
</models>
</global>
</config>
Selanjutnya Anda harus menambahkan JS ke kepala tema Anda. Di bawah app / design / frontend / default / YOURTHEME / template / halaman / html / head.phtml tambahkan ini di akhir. Jika Anda tidak memiliki file ini, salin dari file dasar. Jangan menimpa file dasar. Selalu buat sendiri!
<?php
/* reCAPTCHA */
if ( strpos( Mage::helper('core/url')->getCurrentUrl(), 'account/create') != false ) { ?>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<?php } ?>
Sekarang di app / design / frontend / default / YOURTHEME / template / persistent / customer / form / register.phtml tambahkan ini tepat sebelum div set-tombol di dekat bagian bawah:
<div class="g-recaptcha" data-sitekey="your-site-key-goes-here"></div>
<span id="captcha-required" style='display:none; color:#ff0000'><?php echo $this->__('Please Fill Recaptcha To Continue'); ?></span>
Hampir selesai! Sekarang cukup daftarkan modul baru Anda dengan membuat aplikasi / etc / modules / YourVendorName / ValidateCaptcha.xml dengan yang berikut:
<?xml version="1.0"?>
<config>
<modules>
<YourVendorName_ValidateCaptcha>
<active>true</active>
<codePool>local</codePool>
</YourVendorName_ValidateCaptcha>
</modules>
</config>
Ganti YourVendorName dengan yang Anda inginkan. Struktur akhir Anda harus seperti:
- app
- code
- local
- YourVendorName
- ValidateCaptcha
- etc
config.xml
- Model
Customer.php
- design
- frontend
- default
- YOURTHEME
- template
- customer
- form
register.phtml
- page
- html
head.phtml
- persistent
- customer
- form
register.phtml
- etc
- modules
YourVendorName_ValidateCaptcha.xml