php 登录 Magento 后将客户重定向到自定义 URL/页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12815773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:17:06  来源:igfitidea点击:

Redirect A Customer To A Custom Url/Page After Logging In Magento

phpmagentomagento-1.7

提问by Shahid Ahmed

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.

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

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

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

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.

1)我在同一个域上。

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

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

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.

我想在重定向到登录页面之前设置返回 url。所以登录后它会将用户重定向到返回 url 页面。& 我的自定义页面在 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!!

请帮忙!!

回答by Deus777

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.

我遇到了类似的问题,我使用了不同的解决方案。在我的场景中,Magento 将用户重定向到最后一页,他在上次登录时处于打开状态。

At first it was confusing because even after setting Admin > System > Configuration > Customer Configuration > Login Options > Redirect Customer to Account Dashboard after Logging into NOi 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 alwaysredirect user after logging, to the last page he currently was on.

无论如何,我希望 Magento在登录后始终将用户重定向到他当前所在的最后一页。

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

我想避免安装任何扩展,或者自己创建额外的扩展(这包括重写 AccountController)。所以我只是通过本地覆盖 Magento/Customer/Model/Session.php 来解决它,我在其中添加了 $this->unsBeforeAuthUrl(); 在登录方法中(成功验证后)。

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.

多亏了这一点,每次用户登录 before_auth_url 被清除,这迫使 magento 将用户重定向到存储在引用参数中的 url。

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

我不得不在我的 mini.login.phtml 表单中添加引用参数。这是这样做的。

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

首先在 template/customer/form/mini.login.phtml 的顶部添加:

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

我不确定它是否完美的解决方案(因为它需要添加这个引用跟踪)——也许 Magento 在内部存储了其他地方的最后一个 url,并且可以从会话中读取它。

回答by Maniprakash Chinnasamy

Try the below code for redirection

尝试以下代码进行重定向

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

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

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

回答by Z. Rahman Raju

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

打开 \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.

环顾第 187 行。Mage::helper('customer')->getAccountUrl()是到客户仪表板的重定向 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

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

回答by Ajay

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

登录、注销和注册后重定向是 magento 中非常常见的问题。请找到下面的代码,它可以帮助您。

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

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

“Customurl”是您登录后要重定向的网址。

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

如果您想在登录、注销和注册后为您的电子商务网站提供自定义 url 重定向的完整解决方案。自定义重定向扩展可以帮助您。单击链接以获取扩展名。http://www.magentocommerce.com/magento-connect/custom-redirection.html

回答by dgianco

Basically use setBeforeAuthUrl

基本上使用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

问候

回答by Deepak Mallah

try this

尝试这个

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