1

我在magento中有一个自定义页面。我的条件是“如果用户没有登录,所以在保存任何更改之前我将用户重定向到登录页面,我想在登录后将用户重定向到我的自定义页面上。”。我正在使用以下代码,登录后它不会在我的自定义页面上重定向我。

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");    
}

它在登录页面上重定向我。如果我使用以下代码而不是header它不会将我重定向到登录页面。

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

或者

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

1)我在同一个域上。

2)“系统”>“配置”>“客户配置”部分“登录选项”->“登录后将客户重定向到帐户仪表板”设置为否。

我想在重定向到登录页面之前设置返回 url。所以登录后它会将用户重定向到返回 url 页面。&我的自定义页面在magento之外。

这是我的自定义页面代码。

$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'));  
}

请帮忙!!

4

6 回答 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.

于 2013-11-06T11:22:09.320 回答
3

尝试以下代码进行重定向

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

在 Magento_redirect中是页面重定向的属性。应用您的自定义页面 url 而不是使用 page_url.

于 2012-10-10T09:15:55.967 回答
2

第一的:

转到管理员 > 系统 > 配置 > 客户配置 > 登录选项 > 将否设置为“登录后将客户重定向到帐户仪表板”

然后:

打开 \app\code\core\Mage\Customer\controllers\AccountController.php

环顾第 187 行。Mage::helper('customer')->getAccountUrl()是指向客户仪表板的重定向 url。将此更改为您想要的网址。

即你可以改变:

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

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

成功登录后将客户重定向到主页

于 2013-10-09T04:43:10.620 回答
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

于 2014-11-29T12:43:42.140 回答
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

于 2014-04-10T17:35:56.520 回答
0

try this

<?php Mage::getSingleton('customer/session')->setBeforeAuthUrl('your_url'); ?>
于 2014-06-30T10:16:56.223 回答