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.