Saturday, February 16, 2013

Magento: Redirect to login page if user not login

Here is simple magento code to redirect to login page, if user is not logged in:

    $redirect_url = Mage::getUrl('customer/account/login/');
    $current_url = Mage::helper('core/url')->getCurrentUrl();
    if((!$this->helper('customer')->isLoggedIn()) && ($current_url != $redirect_url)){

Use this code and enjoy.. :)


vaseem ansari said...

This code is having a bug.
Once you use this code then user will not be able to register himself.
I wrote a more useful code for this.

Anonymous said...

vaseem ansari,
could you show us your code?
thank you

kamal mehta said...

I will check this code ,thanks for sharing this mohit.

Magento Developers

Anonymous said...

I am a magento Developer
Can you tell me where should i put my login code so that it will ask me for login in after clicking on any category when i am in home page .
If possible please provide me the code also.

plz send me the code in


Can you please elaborate your question.


Anonymous said...

I am in Home Page.There 3-4 category.Now when a customer is not login and if he try to click on any one of category of in in home page to see the product he wil not able to see it rather it will be redirect to its account page to login first.

Anonymous said...
I want to make like this


Thanks for comment.

Try this code:

Go to:

Open app/design/frontend/[your_package]/[your_theme]/template/catalog/category/view.phtml

and paste below code at starting of file:

$module = $req->getModuleName();
$controller = $req->getControllerName();
$redirect_url = Mage::getUrl('customer/account/login/');
//$current_url = Mage::helper('core/url')->getCurrentUrl();
if((!$this->helper('customer')->isLoggedIn()) && ($module=='catalog') && ($controller=='category')){

Hope this will help you.


Anonymous said...

Thanks a lot... for guide me.
But this code is not working as because under catalog folder there is no category folder so that i can put the view.phtml file rather there is product folder under which view.phtml is present already..
so please give a solution


You should copy file from

app/design/frontend/base/default/template/catalog/category folder


make a folder named "category" in

app/design/frontend/[your_package]/[your_theme]/template/catalog folder.

then use this view.phtml file for your code.

Let me know, if you find any problem.


Anonymous said...

Its still giving error..
Fatal error: Call to undefined function getRequest() in C:\xampp\htdocs\magento\app\design\frontend\ultimo\default\template\catalog\category\view.phtml on line 35

what is getRequest()and where should it will be called?
please help


Sorry !

Its typo mistake.

There should be:

$req = Mage::app()->getRequest();

in place of:

getRequest() only.

Anonymous said...

Thanks dear its working fine now...
Thanks a lot.
But the url structure is

instead of

can u explain why ?___SID=U is come?

Anonymous said...

I have got an error "Internal Server Error" occuring during magento installation after uplaoding my folder in the server.
please suggest me what can I do?

Which file permission do I have to Change?
please suggest me

Anonymous said...

I haven't got your reply for this ..
Please help me to work out..

I have got an error "Internal Server Error" occuring during magento installation after uplaoding my folder in the server.
please suggest me what can I do?

Which file permission do I have to Change?
please suggest me


To overcome 500 Internal Server Error :

change the folder permission of below folder to 777
change the file permission of below files from 664 to 644
index.php (main index file in magento root folder)
downloader/index.php ( otherwise if you will try to access System > Magento Connect >Magento Connect Manager (after magento installation) by logging to magento admin, you will get 500 Internal Server Error.)

If still you are getting problem, try this:

Download magento-cleanup tool (URL: and extract t to your project's root folder.
Then run the extracted file in browser to see if it solve your problem.

Hope it will solve your problem.

Anonymous said...

Can U tell me how to make Guest user login in magento?

I mean to say thatI want to make 2 user type for login :

1) Registered User (Which Comes Default after installing Magento).

2)Guest User(Which I have to do).
When a particular user click on Guest user it ask for username and password for login.


How can a guest user login, when there are no credentials of user available in the database.

Please explain your problem a bit more.

Anonymous said...

Thanks for your comment...
I have created 3 website under 1 admin panel.
Now i am trying to make 3 login section for the 3 website in the frontend. How can I do that.

Anonymous said...

I haven't got any reply from your end the above query

I have created 3 website under 1 admin panel.
Now i am trying to make 3 login section for the 3 website in the frontend. How can I do that.

If you can able to answer then please help me to solve the above problem....


Why do you want to make 3 different login sections for your three websites.

whenever you will access a particular website on frontend, there you will be able to login to that particular site.

Please explain a little about your problem, so that I can try to understand the login behind different logins.

Anonymous said...

As i have explain that I have created 3 website under same domain.

I have created store for individual website.

I know the login option of the 3 website will be the same.I dont have to make different login box.

just for example:

When I go :localhost/magento/shoe
there will be a login option.
Any customer go to that site and he can registered as well he can entered into the the shoe website.

Similar way for next two the same case will occur.
So here we dont need to make different login boxes.

Upto that section i have done.

Now there is requirement to have a Retail Shop:
Under localhost/magento(Which is the main domain) there will a section for retailer to create an account and to login into his account.
This retailer has the option to control above all 3 website as mentioned above.

For this reason I want another login option for this retail shop.
How can i do that.Please guide me

If you still have doubt then please let me know... Thanks in advance


In this case, you need a new seller section.

That means, when user registers as retailer, he/ she will be as a normal frontend user.

When user registers for retailer and admin approves him as retailer, he will be able to login from his area and then he can manage his inventory and orders.

For this, I think you need to override admin section.

First create a new role "retailer" from admin section & assign only products, sales section to it.

When user registers as retailer, he should be assigned "retailer" role.

Now this new seller will have access to all products and orders.

Now you need to override catalog module and sales module to restrict retailer to his relevant items only.

I think this clears your problem's solution. Let me know if you need some assistance.

Andie said...
This comment has been removed by the author.
Andie said...

Great Read! I am impressed on how you make your article easy to understand. I'll come back for more :D

For more tutorial and tips about web design and web development you can follow me here ->