Showing posts with label force. Show all posts
Showing posts with label force. Show all posts

Wednesday, October 5, 2011

How to prevent not logged in users from accessing magento site and force customer to login?

Step 1:
Create and put below content to the file named "redirect.phtml" and place under "/template/page/html/".
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; 
} 
Step 2:
Edit "page.xml" and place the code

after

Step 3:
Edit the below files "template/page/"
1. 1column.phtml
2. 2columns-left.phtml
3. 2columns-right.phtml
4. 3columns.phtml

and Insert the following line at the start of all the files.
echo $this->getChildHtml('auth-redirect')
Step 4:
Now Add exceptions for home, login and cms page.
Edit your "layout/customer.xml"
Add below code
<remove name="auth-redirect" />
under
<customer_account_login>
Also If you want users to be able to create an account, another good one to make public might be place code under
<customer_account_create>
in "layout/customer.xml"
If you want to make the home (front) page public, add the "remove" code to the 

<cms_index_index>
That's it you are done.