Friday, March 22, 2013

Prevent visitors to view the store without login in Magento


>> Magento use methods :  _redirect() AND _redirectUrl()
for redirect in Mage_Core_Controller_Varien_Action controller. If you see these methods, they are using this code to redirect $this->getResponse()->setRedirect('your_url');

Now you just need to call like :
                        Mage::app()->getFrontController()
                               ->getResponse()
                               ->setRedirect(Mage::getBaseUrl('your_url');


I used this code to prevent visitors to view my store without login.
With the following code in head.phtml :

<?php
    $CusSession=mage::getSingleton('customer/session');
    $frontController = Mage::app()->getFrontController();
  
    if(!$CusSession->isLoggedIn() && $frontController->getRequest()->getActionName() != 'login')
    {
       $frontController->getResponse()->setRedirect(Mage::getUrl("customer/account/login"));
    }
?>




No comments:

Post a Comment