How to Protect Pages So Only Logged in Users Can View Them in magento

Create a auth-redirect.phtml template with below contents

<?php
Mage::getSingleton('customer/session')->setBeforeAuthUrl($this->getRequest()->getRequestUri());  //save requested URL for later redirection
if(!Mage::getSingleton('customer/session')->isLoggedIn()) {  // if not logged in
    header("Status: 301");
    header('Location: '.$this->getUrl('customer/account/login')) ; // send to the login page
    exit;
} ?>


Right now the 301 redirects to the login page, but it could be the homepage or somewhere else as well.

then Edit page.xml of your current theme..

<block type="page/html" name="auth-redirect" as="auth-redirect" template="page/html/auth-redirect.phtml"/>

 Insert this as the first item under the “root” Block (i.e. <block type=“page/html” name=“root” output="toHtml">)

Now its time to Edit page layout templates

like 1column.phtml,2columns-left.phtml in template/page directory...

put <?php echo $this->getChildHtml('auth-redirect') ?> to top line after <?php tag.

Finally, we need to add “exceptions” – public pages like the “login” page,customer register page etc which don’t need to be protected. On a site that is only accessible to logged in users so thia feature you have to edit customer.xml file in particulat layout handle for example <customer_create_account> and put into the layout handler

<remove name="auth-redirect" />

that's it

Thank You!

 



Comments