Categories
Magento PHP

Magento : How to Redirect Non Logged in Users to Login Page

There are some many ways to redirect users/visitors in magento. But the most easy way i found is written below. You can modify the head phtml file and the redirection will be done automatically.

There are some many ways to redirect users/visitors in magento. But the most easy way i found is written below.
To redirect the users/visitors to login page you can add below line of code in head.phtml of your current theme.

<?php		// Added by Vaseem to redirect non logged in user to login page
$currentUrl = $this->helper('core/url')->getCurrentUrl();
echo 'currentUrl->'.$currentUrl.'<br>';
$loginStatus	=	'';
if ( strpos( $currentUrl, "customer" ) !== false ) {
	$loginStatus	=	1;
}
if(!$this->helper('customer')->isLoggedIn() && $loginStatus!=1){
	echo 'redirect to login page now';
  	Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('customer/account/login/'));
}
?>

Stay in touch

For a daily email update, click HERE or enter your email and stay on top of things by receiving weekly news in your inbox.

By Vadim

I am Vadim Ansari, a software engineer by profession and a passionate coder. I live every moment at the fullest. I am especially interested in WordPress as blogging platform. When I am not chilling around with friends, I stay occupied with one of the php projects. You can follow me on these social networks Facebook, Twitter and Linkedin.

2 replies on “Magento : How to Redirect Non Logged in Users to Login Page”

Pretty nice post. I just stumbled upon your weblog and wished to say that I’ve truly enjoyed surfing around your blog posts. In any case I’ll be subscribing to your feed and I hope you write again very soon!

Leave a Reply

Your email address will not be published. Required fields are marked *