Expertwebadvisor

  • Magento 2
  • PHP
  • WordPress
  • MySQL
  • SEO
  • Java Script
  • Others
  • Top 10’s

Force redirection on the login page in Magento

By Expertwebadvisor | Last Updated: March 6, 2014 | In: Magento


Many times when you want all the website or some part of your Magento store to only be visible to customers who are logged in on the website. May be you are developing the store for the wholesale user and there may only approved wholesale users should be able to view products and checkout. This is not the default functionality of the Magento but you can do it just by using the steps given below:
Force redirection on the login page in Magento

Step 1. Create phtml file for the redirect handling

First of all create a phtml file into the app/design/frontend/base/default/template/page/ or app/design/frontend/default/your_custom_theme/template/page/ directory of your active theme. Call it something like redirect-auth.phtml and copy and paste the below code into it:

<?php
	//echo "Authorize Page";
	Mage::getSingleton('customer/session')->setBeforeAuthUrl($this->getRequest()->getRequestUri()); 
	if(!Mage::getSingleton('customer/session')->isLoggedIn()) 
	{ 
		header("Status: 301"); 
		header('Location: '.$this->getUrl('')); 
		exit; 
	} 
?>

From the above code the 301 redirects is on the home page you can redirect the user on the login page or somewhere else as well just by changing the header location code Suppose you want to redirect on the Login page replace this header(‘Location: ‘.$this->getUrl(”)); by the code header(‘Location: ‘.$this->getUrl(‘customer/account/login’)); .

Step 2. Editing of the page.xml

For the redirection available to all of the page templates on our website we need to modify the page.xml. For this navigate to the app/design/frontend/base/default/layout/ or app/design/frontend/default/your_custom_theme/layout/page.xml in your active theme, add the following line of code:

below the code

<block type="page/html" name="root" output="toHtml" template="page/3columns.phtml">

Now the final code will be :

<default translate="label" module="page">
<label>All Pages</label>
<block type="page/html" name="root" output="toHtml" template="page/3columns.phtml">
	<block type="page/html" name="redirect-auth" as="auth-redirect" template="page/redirect-auth.phtml"/>
.......................
.......................
.......................

Step 3. Now Editing of the page layout templates:

Now we need to include the below code for all of the page templates i.e.

1column.phtml
2columns-left.phtml
2columns-right.phtml
3columns.phtml
and others

Insert the following line of the code on the top of the file into all of the available templates

getChildHtml(‘redirect-auth’); ?>

which ensures that the redirect is included before any of the page HTML renders. You have almost done but there may be some pages which don’t need to be protected just like as the contact form login and register page, for making these page public we need some exception code which you have to insert into the appropriate xml files.

Let me explain it here with one of the example suppose you want to public the contact form then we must have to make the changes into the contacts.xml file for this navigate to the app/design/frontend/base/default/layout/ or app/design/frontend/default/your_custom_theme/layout/contacts.xml and add the below code

into it after the it will look like as :

<reference name="root">
<remove name="redirect-auth" />
    <action method="setTemplate"><template>page/2columns-right.phtml</template></action>
    <action method="setHeaderTitle" translate="title" module="contacts"><title>Contact Us</title></action>
</reference>

Thats it and now your user will be able to see the contact form.

Hope it helps, Thanks for reading and enjoy the Magento custom coding. 😉

Tags:Force Redirection in Magento,Force redirection on any page In Magento,Magento,Redirection on the login page in Magento


Related Posts

  • Implement WYSIWYG editor (TinyMCE) in custom Magento Admin Module Implement WYSIWYG editor (TinyMCE) in custom Magento Admin Module
  • Change Admin URL of the Existing Magento Store Change Admin URL of the Existing Magento Store
  • How To Call Core Template File In Magento How To Call Core Template File In Magento
  • Related products on product description page in Magento Related products on product description page in Magento
  • Be Social, Share Us Here !!

  • Looking For Something !!

  • Popular Categories !!

    • Magento 2 Guide and Tutorials
    • Home Loan EMI Calculator
    • Car Loan EMI Calculator
    • Personal Loan EMI Calculator
    • Magento Guide & Tutorials
    • PHP Tutorials
    • WordPress Tutorials
    • SEO Tutorials
    • MySQL Guide
    • Opencart Tutorials
    • Linux/Ubuntu Guide
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Facebook
  • Twitter
  • DMCA.com Protection Status

Recent Posts

  • PHP array_​diff_​key Function
  • PHP array_​diff_​assoc Function
  • PHP array_​diff Function
  • PHP array_​count_​values Function
  • PHP array_​combine Function

Welcome to my Blog

Thanks for visiting my Blog, I have started this blog before 8 years ago and sharing my own experience, problem and solutions while working on the different type of projects. Enjoy the reading and don't forget to Facebook likes, Tweets or G+. You can also write me expertwebadvisor@gmail.com
© 2013 - 2025 Powered By
Top