Magento Forum

   
Redirect back to page after logged-in in Magento
 
pradeep_kumar67
Sr. Member
 
Avatar
Total Posts:  160
Joined:  2010-06-24
Bangalore
 

If you need to go back to same page once it logged in ,
that is
your in list page and clicked to sign in link and then you logged in it has to go back to list page its self

Magento has default setting for not to redirect account page if you logged in from default page by defult it will redirect to account dash board
but you need to go page to list page
for that follow the below steps

Open header.phtml add this code at top of the file

<?php
/*pradeep start */
 
if(!Mage::helper(\'customer\')->isLoggedIn()) {
 $loginback
=$this->helper(\'core/url\')->getCurrentUrl();
 
$check=strstr($loginback\'customer/account/login\');
 if(!
strlen($check)){
    Mage
::getSingleton(\'core/session\')->setLoginBackUrl($loginback);
 
}
}

/*pradeep end */

?>

override app\\code\\core\\Mage\\Customer\\controllers\\AccountController.php to you local or custom module and add this function

<?php
    
    
/**
     * Define target URL and redirect customer after logging in
     */
    
public function _loginPostRedirect()
    
{
        $session 
$this->_getSession();

        if (!
$session->getBeforeAuthUrl() || $session->getBeforeAuthUrl() == Mage::getBaseUrl()) {

            
// Set default URL to redirect customer to
            
$session->setBeforeAuthUrl(Mage::helper(\'customer\')->getAccountUrl());
            
// Redirect customer to the last page visited after logging in
            
if ($session->isLoggedIn()) {
            
/*pradeep start */
            
$backUrlcustom=Mage::getSingleton(\'core/session\')->getLoginBackUrl();
            
/*pradeep end */
                
if (!Mage::getStoreConfigFlag(\'customer/startup/redirect_dashboard\')) {
                    $referer 
$this->getRequest()->getParam(Mage_Customer_Helper_Data::REFERER_QUERY_PARAM_NAME);
                    if (
$referer{
                        $referer 
Mage::helper(\'core\')->urlDecode($referer);
                        if (
$this->_isUrlInternal($referer)) {
                            $session
->setBeforeAuthUrl($referer);
                        
}
                    }
                } 
else if ($session->getAfterAuthUrl()) {
                    $session
->setBeforeAuthUrl($session->getAfterAuthUrl(true));
                
/*pradeep start */
                
else if (strlen($backUrlcustom)) {
                    Mage
::getSingleton(\'core/session\')->setLoginBackUrl(\'\');
                    
$session->setBeforeAuthUrl($backUrlcustom);
                
}
                
/*pradeep emd */
            
else {
                $session
->setBeforeAuthUrl(Mage::helper(\'customer\')->getLoginUrl());
            
}
        } 
else if ($session->getBeforeAuthUrl() == Mage::helper(\'customer\')->getLogoutUrl()) {
            $session
->setBeforeAuthUrl(Mage::helper(\'customer\')->getDashboardUrl());
        
else {
            
if (!$session->getAfterAuthUrl()) {
                $session
->setAfterAuthUrl($session->getBeforeAuthUrl());
            
}
            
if ($session->isLoggedIn()) {
                $session
->setBeforeAuthUrl($session->getAfterAuthUrl(true));
            
}
        }
        $this
->_redirectUrl($session->getBeforeAuthUrl(true));
    
}
    
    ?>

 
Magento Community Magento Community
Magento Community
Magento Community
 
pradeep_kumar67
Sr. Member
 
Avatar
Total Posts:  160
Joined:  2010-06-24
Bangalore
 

it work fines if some one know better and simple code then this code please post it in this post

pradeep_kumar67 - 02 January 2013 10:22 PM

If you need to go back to same page once it logged in ,
that is
your in list page and clicked to sign in link and then you logged in it has to go back to list page its self

Magento has default setting for not to redirect account page if you logged in from default page by defult it will redirect to account dash board
but you need to go page to list page
for that follow the below steps

Open header.phtml add this code at top of the file

<?php
/*pradeep start */
 
if(!Mage::helper(\'customer\')->isLoggedIn()) {
 $loginback
=$this->helper(\'core/url\')->getCurrentUrl();
 
$check=strstr($loginback\'customer/account/login\');
 if(!
strlen($check)){
    Mage
::getSingleton(\'core/session\')->setLoginBackUrl($loginback);
 
}
}

/*pradeep end */

?>

override app\\code\\core\\Mage\\Customer\\controllers\\AccountController.php to you local or custom module and add this function

<?php
    
    
/**
     * Define target URL and redirect customer after logging in
     */
    
public function _loginPostRedirect()
    
{
        $session 
$this->_getSession();

        if (!
$session->getBeforeAuthUrl() || $session->getBeforeAuthUrl() == Mage::getBaseUrl()) {

            
// Set default URL to redirect customer to
            
$session->setBeforeAuthUrl(Mage::helper(\'customer\')->getAccountUrl());
            
// Redirect customer to the last page visited after logging in
            
if ($session->isLoggedIn()) {
            
/*pradeep start */
            
$backUrlcustom=Mage::getSingleton(\'core/session\')->getLoginBackUrl();
            
/*pradeep end */
                
if (!Mage::getStoreConfigFlag(\'customer/startup/redirect_dashboard\')) {
                    $referer 
$this->getRequest()->getParam(Mage_Customer_Helper_Data::REFERER_QUERY_PARAM_NAME);
                    if (
$referer{
                        $referer 
Mage::helper(\'core\')->urlDecode($referer);
                        if (
$this->_isUrlInternal($referer)) {
                            $session
->setBeforeAuthUrl($referer);
                        
}
                    }
                } 
else if ($session->getAfterAuthUrl()) {
                    $session
->setBeforeAuthUrl($session->getAfterAuthUrl(true));
                
/*pradeep start */
                
else if (strlen($backUrlcustom)) {
                    Mage
::getSingleton(\'core/session\')->setLoginBackUrl(\'\');
                    
$session->setBeforeAuthUrl($backUrlcustom);
                
}
                
/*pradeep emd */
            
else {
                $session
->setBeforeAuthUrl(Mage::helper(\'customer\')->getLoginUrl());
            
}
        } 
else if ($session->getBeforeAuthUrl() == Mage::helper(\'customer\')->getLogoutUrl()) {
            $session
->setBeforeAuthUrl(Mage::helper(\'customer\')->getDashboardUrl());
        
else {
            
if (!$session->getAfterAuthUrl()) {
                $session
->setAfterAuthUrl($session->getBeforeAuthUrl());
            
}
            
if ($session->isLoggedIn()) {
                $session
->setBeforeAuthUrl($session->getAfterAuthUrl(true));
            
}
        }
        $this
->_redirectUrl($session->getBeforeAuthUrl(true));
    
}
    
    ?>

 
Magento Community Magento Community
Magento Community
Magento Community
 
pradeep_kumar67
Sr. Member
 
Avatar
Total Posts:  160
Joined:  2010-06-24
Bangalore
 

This one of alternate solution

create you custom module then in etc/config.xml add this code

<events>
           <
customer_login>
               <
observers>
                   <
loginredirect>
                       <class>
custommodule/observer</class>
                       <
method>customerLogin</method>
                   </
loginredirect>
               </
observers>
           </
customer_login>
       </
events>

in observer.php add this code

public function customerLogin(Varien_Event_Observer $observer)
   
{
       
if (Mage::helper('loginredirect')->isEnabled()) {
           $_session 
$this->_getSession();
           
$_session->setBeforeAuthUrl(Mage::helper('custommodule')->getRedirectUrl());
       
}
   }

   
/**
    * @return Mage_Core_Model_Abstract
    */
   
protected function _getSession()
   
{
       
return Mage::getSingleton('customer/session');

and in helper add this code

/**
    * Return path for redirect
    *
    * @return string
    */
   public function getRedirectUrl()
   
{
       $_path 
= (string) $this->_getConfigValue('path_redirect');
       return 
Mage::getUrl($_path);
   
}

i got this solution from

http://blog.belvg.com/magento-tips-how-to-redirect-a-user-to-other-page-than-his-account-page-after-login-in-magento.html?goback=&#x2E;gde_3543622_member_217595208

 
Magento Community Magento Community
Magento Community
Magento Community
 
Aries86
Jr. Member
 
Total Posts:  1
Joined:  2013-05-09
 

Yes I agree, I just finished reading it and posted a question relating to it.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top