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

时间:2012-10-10 09:09:06

标签: php magento magento-1.7

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

OR

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

请帮助!!

6 个答案:

答案 0 :(得分:5)

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

起初它很混乱,因为即使在设置 Admin>之后系统>配置>客户配置>登录选项>登录后将客户重定向到帐户信息显示板 我仍然被重定向到信息中心。

最后我意识到这就是我的情况,这是我最近退出后的最后一页。

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

我想避免安装任何扩展程序,或者创建我自己的附加扩展程序(这包括重写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;
}

感谢现在每次用户登录before_auth_url时,会强制magento将用户重定向到存储在referer参数中的url。

我必须在我的mini.login.phtml表单中添加referer参数。这是这样做的。

首先在模板/ 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;
?>

然后我在里面的某处添加:

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

现在它以我想要的方式工作(至少现在,我今天创造了这个)。当我遇到这个解决方案的一些问题时,我会尝试添加一些注释。

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

答案 1 :(得分:3)

尝试以下代码进行重定向

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

_redirect是页面重定向的属性。应用您的自定义网页网址,而不是使用page_url

答案 2 :(得分:2)

<强>第一

转到管理员&gt;系统&gt;配置&gt;客户配置&gt;登录选项&gt;设置为“登录后将客户重定向到帐户控制面板”

,然后

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

查看#187行。Mage::helper('customer')->getAccountUrl()是客户信息中心的重定向网址。将其更改为您想要的网址。

即。你可以改变:

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

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

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

答案 3 :(得分:1)

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

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

&#34; Customurl&#34;是登录后要重定向的网址。

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

答案 4 :(得分:0)

基本上使用 setBeforeAuthUrl

我使用此代码重定向到referer

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

例如,在自定义登录表单中:

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

此致

答案 5 :(得分:0)

试试这个

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