以编程方式将用户登录到Magento [英] Log user into Magento programmatically

查看:85
本文介绍了以编程方式将用户登录到Magento的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够自动将现有客户登录到Magento,然后将其重定向到已登录的实时Magento站点.该站点位于同一服务器上的两个子域之间.登录将在app.mydomain.com(本身只是一个PHP应用程序;不是Magento站点)上进行,然后Magento安装在shop.mydomain.com.

I would like to be able to log an existing customer into Magento automatically and then redirect them to the live Magento site, logged in. This is between two subdomains on the same server. The login will happen on app.mydomain.com (which is itself just a PHP app; not a Magento site), and then the Magento installation is at shop.mydomain.com.

我尝试了很多这样的排列,但是没有运气.这是我当前的代码:

I've tried a couple dozen permutations of this with no luck. Here's my current code:

// Include Magento app
require_once(config::$magento_root . '/app/Mage.php');
Varien_Profiler::enable();
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('default');

// Initialize Magento session
Mage::getSingleton('core/session', array('name' => 'frontend'));

// Get instance of customer model for the actual website
$customer = Mage::getModel('customer/customer')->setWebsiteId(Mage::app()->getStore()->getWebsiteId());

// Load the client with the appropriate email
$customer->loadByEmail($email_address);

// Get a customer session
$session = Mage::getSingleton('customer/session');

// Login and check the customer by his uid
$session->loginById($customer->getId());

// Redirect to shop home page
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getBaseUrl())->sendResponse();

这确实设法使用户登录($session->loginById()返回1),但是在重定向后,该客户再次注销.在执行任何上述操作之前,我已经尝试过使用session_destroy().我曾尝试将Magento的Cookie域更改为.mydomain.com,但目前还没有任何效果.这有可能吗?

This does manage to log the user in ($session->loginById() returns 1), but upon redirect, the customer is logged out again. I've tried using session_destroy() before doing any of this; I've tried changing Magento's cookie domain to .mydomain.com, but nothing has worked yet. Is this even possible?

推荐答案

即使两个子域位于同一服务器上,我也认为不可能在两个子域之间执行此操作.因此,经过一番思考,并根据史蒂夫·罗宾斯(Steve Robbins)的评论,我想出了另一个(hacky)解决方案.

I don't think it's possible to do this between two subdomains, even if they are on the same server. So after some thought, and based on Steve Robbins' comment, I came up with another (hacky) solution.

在我的(非Magento)应用程序中,我查找了想要登录的Magento用户ID并对其进行了加密.然后,我在Magento安装的根目录中创建了一个PHP文件,该文件将解密该用户ID,登录到Magento,然后将用户重定向到Magento的首页.完成所有这些之后,只需将加密的用户ID通过带有querystring参数的重定向从应用程序传递到PHP文件即可.

From my (non-Magento) app, I looked up the Magento user ID I wanted to log in as and encrypted it. Then I created a PHP file in the root directory of my Magento installation that would decrypt that user ID, log into Magento, and redirect the user to the front page of Magento. After all this, it was simply a matter of passing that encrypted user ID from the app to the PHP file via a redirect with a querystring argument.

我认为最好使用自定义的Magento模块来处理路由,而不是将PHP文件放在服务器上...但是我不知道如何编写Magento模块,时间紧迫.

I think this could probably better be done with a custom Magento module to handle the routing, instead of a PHP file sitting on the server... but I don't know how to write a Magento module and we're in a time crunch.

这篇关于以编程方式将用户登录到Magento的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆