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();
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?