1

I have one custom page in magento.My condition is that "if user is not loggedin so before saving any changes I am redirecting user to the login page, I want to redirect the user on my custom page after logging.". I am using the following code its not redirecting me on my custom page after logging.

Mage::app('default'); if( !Mage::getSingleton( 'customer/session' )->isLoggedIn() ){ $session = Mage::getSingleton( 'customer/session' ); $session->setBeforeAuthUrl('http://'.$_SERVER['HTTP_HOST'].'/custom.html'); header("Location: /customer/account/login"); } 

its redirecting me on the login page. if I use following code instead of header it wont redirecting me to the login page.

Mage::app()->getResponse()->setRedirect(Mage::getUrl("customer/account/login")); 

OR

Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('customer/account')); 

1) I am on the same domain.

2) "System" > "Configuration" > "Customer Configuration" section "Login Options" -> "Redirect Customer to Account Dashboard after Logging" is set to No.

I want to set the return url before redirecting to the login page. so after login it will redirect the user to the return url page. & My custom page is outside of magento.

Here is my custom page code.

$mageFilename = 'app/Mage.php'; require_once( $mageFilename ); umask(0); Mage::app(); if( !Mage::getSingleton( 'customer/session' )->isLoggedIn() ){ $session = Mage::getSingleton( 'customer/session' ); $session->setBeforeAuthUrl('http://'.$_SERVER['HTTP_HOST'].'/full-custom.php?sid=8'); header("Location: /customer/account/login"); //Mage::app()->getResponse()->setRedirect(Mage::getUrl("customer/account/login")); //Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('customer/account')); } 

Please help!!

6 Answers 6

5

I've had a similar problem, and I used different solution. In my scenario Magento redirected user to the last page, he was on while being logged the last time.

At first it was confusing because even after setting Admin > System > Configuration > Customer Configuration > Login Options > Redirect Customer to Account Dashboard after Logging in to NO i was still being redirected to dashboard.

Finally I realized that that was in my case exactly the last page I was on after logging out recently.

Anyway, I wanted Magento to always redirect user after logging, to the last page he currently was on.

I wanted to avoid installing any extensions, or creating additional extension my own (this includes rewriting AccountController). So I simply solved it by local overwrite of Magento/Customer/Model/Session.php where I added $this->unsBeforeAuthUrl(); in login method (after successful authenticate).

public function login($username, $password) { /** @var $customer Mage_Customer_Model_Customer */ $customer = Mage::getModel('customer/customer') ->setWebsiteId(Mage::app()->getStore()->getWebsiteId()); if ($customer->authenticate($username, $password)) { $this->unsBeforeAuthUrl(); // <-- my addition $this->setCustomerAsLoggedIn($customer); $this->renewSession(); return true; } return false; } 

Thanks to this now each time user is logged before_auth_url is cleared, which forces magento to redirect user to url stored in referer parameter.

And I had to add referer parameter to my mini.login.phtml form. Which is done like this.

At first on the top of template/customer/form/mini.login.phtml I add:

<?php $params = Mage::helper('customer')->getLoginUrlParams(); $ref = isset($params[Mage_Customer_Helper_Data::REFERER_QUERY_PARAM_NAME])?$params[Mage_Customer_Helper_Data::REFERER_QUERY_PARAM_NAME]:false; ?> 

And then somewhere inside the I add:

<?php if ($ref): ?> <input type="hidden" name="<?php echo Mage_Customer_Helper_Data::REFERER_QUERY_PARAM_NAME ?>" value="<?php echo $ref ?>"/> <?php endif; ?> 

Now it works the way I want to (at least for now, I created this today). I will try to add some comments when I encounter some problems with this solution.

I'm not sure if its perfect solution (since it requires adding this referer tracking) - maybe Magento stores internally last url somewhere else, and could read it from session.

Sign up to request clarification or add additional context in comments.

2 Comments

This is the exact scenario I was looking into for redirecting after login...wish I would have found this hours ago. Your answer was a huge help, thank you!
Happy to help :). Years since then it still works as expected (now in 1.9.2.4). I never encountered any problems with it.
3

Try the below code for redirection

if( !Mage::getSingleton( 'customer/session' )->isLoggedIn() ) { $this->_redirect('page_url'); } 

in Magento _redirect is property for page redirection. apply your custom page url instead of using page_url.

4 Comments

I want to set the return url before redirecting to the login page. so after login it will redirect the user to the return url page. & My custom page is outside of magento.
check the AccountController.php file in below path app/code/core/Mage/Customer/controllers/. And check the _loginPostRedirect function to set your redirection url for customer after login .
This answer doesn't answer the question.
Should i use this code in PHTML file ?Why aren't you specific about your answer? You should write 100% answer.
2

first:

go to admin > System > Configuration > customer configuration > Login Options > set No to "Redirect Customer to Account Dashboard after Logging in"

Then:

open \app\code\core\Mage\Customer\controllers\AccountController.php

look around line # 187. Mage::helper('customer')->getAccountUrl() is the redirection url to customer dashboard. Change this to your desired url.

i.e. you can change:

$session->setBeforeAuthUrl(Mage::helper('customer')->getAccountUrl()); 

To

$session->setBeforeAuthUrl(Mage::getBaseUrl()); 

Which will redirect customers to home page after successful login

4 Comments

Please don't modify core code. Copy the file to app/code/local/Mage/Customer/controllers/AccountController.php if you need to override the core functionality.
With this solution, User will be redirect to home page after login during checkout page.
Never modify the core! And avoid to copy you files to local. Instead, do a clean rewrite, implement your own controller or use a postdispatch event
I liked your solution to setBeforeAuthUrl, but I used it with customer_login observer. You should not change the core. Create a module, and use observers for this.
1

Redirection after logged In, Logged Out and Registration is very common issue in magento. Please find the code below, it can help you.

public function customerLogin() { $_session = Mage::getSingleton('customer/session'); $_session->setBeforeAuthUrl(CustomUrl); } 

"Customurl" is a url on which you want to redirect after Logged In.

If you want complete solution for custom url redirection for your ecommerce website after logged In, Logged Out and Registration. Custom Redirection extension can help you. Click on link to get extension. http://www.magentocommerce.com/magento-connect/custom-redirection.html

Comments

0

Basically use setBeforeAuthUrl

I'm using this code for redirect to referer

<?php Mage::getSingleton('customer/session')->setBeforeAuthUrl($this->getRequest()->getRequestUri());?> 

For example in custom login form:

<form method="post" action="<?php echo Mage::helper('customer')->getLoginPostUrl() ?>"> <?php Mage::getSingleton('customer/session')->setBeforeAuthUrl($this->getRequest()->getRequestUri());?> ... ... .... 

Regards

Comments

0

try this

<?php Mage::getSingleton('customer/session')->setBeforeAuthUrl('your_url'); ?> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.