Google+

Pages

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:

<?php
    $redirect_url = Mage::getUrl('customer/account/login/');
    $current_url = Mage::helper('core/url')->getCurrentUrl();
    if((!$this->helper('customer')->isLoggedIn()) && ($current_url != $redirect_url)){
        Mage::app()->getFrontController()->getResponse()->setRedirect($redirect_url);
    }
?>

Use this code and enjoy.. :)

24 comments:

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

Unknown said...

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

Magento Developers

Anonymous said...

Hi..Mohit
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
mrinalmagento@gmail.com

Mohit Kumar Arora said...

Can you please elaborate your question.

Thanks

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...

http://www.biosculptureshop.co.uk/
I want to make like this

Mohit Kumar Arora said...

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:

getRequest();
$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')){
Mage::app()->getFrontController()->getResponse()->setRedirect($redirect_url);
}
?>

Hope this will help you.

Thanks

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

Mohit Kumar Arora said...

You should copy file from

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

and

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.

Thanks

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

Mohit Kumar Arora said...

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
magento/index.php/customer/account/login/?___SID=U

instead of
magento/index.php/customer/account/login

can u explain why ?___SID=U is come?

Anonymous said...

Hi..
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...

Hi..
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

Mohit Kumar Arora said...

To overcome 500 Internal Server Error :

change the folder permission of below folder to 777
app/etc
var
media
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: http://www.magentocommerce.com/wiki/_media/groups/227/magento-cleanup2.zip) 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...

Hi,
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.

Mohit Kumar Arora said...

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...

Hi..
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....

Mohit Kumar Arora said...

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.
example:
localhost/magento/shoe
localhost/magento/furniture
localhost/magento/computer

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

Mohit Kumar Arora said...

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.