0

I can successfully log a user in like this,

// Include Magento application
require_once ('../store/app/Mage.php');
umask(0);
Mage::app();

// Log in user
$session = Mage::getSingleton('customer/session', array('name' => 'frontend'));
$session->login($username, $password);
$session->setCustomerAsLoggedIn($session->getCustomer());

// redirect with session ID
header('Location: https://www.domain.com/?SID='.$session->getEncryptedSessionId());
exit;

and then I can get any user data I need using,

// Include Magento application
require_once ('../store/app/Mage.php');
umask(0);
Mage::app();

if(Mage::getSingleton('customer/session')->isLoggedIn())
  echo Mage::getSingleton('customer/session')->getId();
else
  echo 'Not logged in';

The problem is that the above block of code returns 'Not logged in' when a user has logged in via the form on the site as opposed to being logged in with the code in the first block.

It appears that the two log in methods create separate sessions that can be run in parallel. If user X logs in using the site's built in form and then user Y logs in using my code - both users are logged in. The store says, welcome user X, but my code returns user Y's ID.

Any ideas as to what's going on? I need users to be able to log in either way, but need to be able to get their ID.

Steve
  • 1,853
  • 16
  • 30

2 Answers2

0

//create new controller in that controller create function say IndexController.php

abc.com/modulename/index/customLogin

public function customLoginAction()
{
if(Mage::getSingleton('customer/session')->isLoggedIn())
   echo Mage::getSingleton('customer/session')->getId();
else{
    echo 'Not logged in';
    $email = 'abc@gmail.com'
    $customer = Mage::getModel('customer/customer');
    $customer->setWebsiteId(Mage::app()->getWebsite()->getId());
    $customer->loadByEmail(trim($email));
    Mage::getSingleton('customer/session')->loginById($customer->getId());
    echo $customer->getId();
  }
}
Manoj Chowrasiya
  • 970
  • 1
  • 13
  • 32
  • Thanks @Manoj. I've only just started working with Magento so sorry if I'm being slow. Can you explain where I put this controller? My install doesn't have the path _abc.com/modulename/index/customLogin_. I've found _IndexController.php_ in _app/code/core/Mage/Core/controllers/_ – Steve Sep 09 '14 at 08:28
0

I'm still not 100% clear what was happening but I have solved the problem - there was two changes I made.

In Magento Admin > System > Configuration > General > Web > Session Cookie Management I set Cookie path to /

In my files where I included the Magento app I added a line of code

// Include Magento application
require_once ('../store/app/Mage.php');
umask(0);
Mage::app();
Mage::getSingleton('core/session', array('name'=>'frontend')); // <- NEW LINE
// ... and so on ...

Now, regardless of how the user gets logged in, my external code and the store both agree on who is logged in.

I can see how changing the cookie path helped but if anyone has any explanation for why the extra line of code worked that would be appreciated.

Cheers!

Steve
  • 1,853
  • 16
  • 30