Eglobe IT Solutions
open

Method to add magento native captcha on product review page

  • Method to add magento native captcha on product review page

    We know how many robots and spam bots browse the internet searching unprotected forms. Therefore, if you have opened form for unregistered users you need to protect it with captcha to avoid spam attacks.

    Magento allows you to include captcha only on certain necessary forms; by default .This blog will create a module to use Magento Native Image Captcha in product review form easily.

    Step1

    Register the module

    Path : app/etc/modules/Egits_Captcha.xml  and add below code :

    
    
        
            
                true
                local
            
        
    
    
    

    Step2

    Create module config.xml file in app/code/local/Egits/Captcha/etc/config.xml

    Register the below events for rendering the display of Captcha.

    
    
        
            
                0.1.0
            
        
        
            
                
                    
                        
                            Egits_Captcha_Model_Observer
                            checkProductReview
                        
                    
                
                
                    
                        
                            Egits_Captcha_Model_Observer
                            insertCaptcha
                        
                    
                
            
        
        
            
                
                    
                        egits_captcha.xml
                    
                
            
        
        
            
                
                    
                        
                            
                        
                    
                
            
            
                
                    
                        1
                    
                
            
        
    
    
    

    Step3

    Please make sure you are using the form's id correct.

    Oberver.php file as below :

    getControllerAction()->getRequest()->getParam('id');
            $returnUrl =  Mage::getUrl('review/product/list', array('id'=> $productId));        
            $formId = 'reviewform';
            $captchaModel = Mage::helper('captcha')->getCaptcha($formId);
           
            $controller = $observer->getControllerAction();
            if (!$captchaModel->isCorrect($this->_getCaptchaString($controller->getRequest(), $formId))) {           
                Mage::getSingleton('core/session')->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
                Mage::app()->getResponse()->setRedirect($returnUrl)->sendResponse();
                exit;
                
            }
        }
        
        public function insertCaptcha($observer)
        {
            $block = $observer->getEvent()->getBlock();
            if ($block instanceof Mage_Review_Block_Form) {
                if (!$block->getChild('review_form_captcha')) {
                    $captcha = Mage::getBlockSingleton('captcha/captcha')->setFormId('reviewform')
                        ->setImageWidth(230)
                        ->setImageHeight(50);
                    $block->setChild('review_form_captcha', $captcha);
                }
            }
        }
    
    }
    
    

    Step4

    Now add the layout XML file in the proper path what your theme suggest.

    Here we have this file under app/design/frontend/base/default/layout/ egits_captcha.xml

    File name as in the config.xml file written before , egits_captcha.xml. This file is to include Javascript file  mage/captcha.js in the head section of review page.

    
    
        
            
                mage/captcha.js
            
        
    
    
    

    Step 5

    Call the captcha on phtml file app/design/frontend/base/default/template/review/form.phtml at the right place and file your theme suggests.

    Here we have used used as below :

    __('Write Your Own Review') ?>

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
      . . . . . . . . . . . . . . .
    • . . . . . . . . . . . . . . .
    • getChildHtml('review_form_captcha'); ?>
    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    Please double check the below , if you felt any difficulties in viewing the captcha image on the preferred position.

    To enable admin panel CAPTCHA, log in to the admin panel and go to System menu>Configuration>Admin button in the Advanced section on the left>CAPTCHA panel on the right. In the CAPTCHA panel there's a drop-down menu Enable CAPTCHA in Admin which is set to No by default. Set it to Yes and some more options will appear under it.

    Thank you for referring this blog!


  • Post Comments

    Submit Comment




    * Required Fields