I have a problem that's driving me crazy - if someone could help with some insight on this, I'd be super grateful:
I have downloadable files in Magento linking to a separate PHP script that generates the downloadable file. I'm trying to only permit access to the file if the user is a) currently logged in, and b) their customer ID matches the ID that was passed to the script.
My problem is with (a):
require_once('app/Mage.php');
umask(0);
Mage::app('');
$s = Mage::getSingleton('customer/session');
if ($s->isLoggedIn()) {
echo 'worked';
}
else echo 'failed';
The problem is that I can't seem to get reliable results from this. If I'm logged in, it says "failed" most of the time (I try logging out, clearing cache, logging back in, etc. and it still says "failed".) It "worked" the first time I tried it for a few minutes, but after logging out, I can't get it to work anymore. Cookie Path is set to / and Cookie Domain is empty, but both Magento and the script are on the same domain.
Any thoughts on where to look/why I can't get reliable results from this?
This code works in our old version so I do not know if it willwork in anything later. This is how we do it now:
umask(0);
Mage::app();
$pidsess = Mage::getSingleton('core/session', array('name'=>'frontend'));
$custsess = Mage::getSingleton('customer/session');
if($custsess->isLoggedIn() == true) {
$LoggedIn = 'yes';
}
unset($piddata);
unset($custsess);
Thank you!!! Working perfectly now. I thought I had tried every possible permutation of how to load Mage, but clearly I missed that one.
Try this:
require_once('app/Mage.php');
umask(0);
Mage::app('default');
Mage::getSingleton('core/session', array('name'=>'frontend'));
if (empty($session))
{
$session = Mage::getSingleton("customer/session");
}
if ($session->isLoggedIn()) {
echo 'worked';
}
else echo 'failed';
Daniel's code is more comprehensive and most liekly woks in newer versions.
Hi Jgoldman,
Your code is valid for Magento 1.
Let try this
require_once('app/Mage.php'); umask(0); Mage::app(); echo Mage::getSingleton('core/session', array('name'=>'frontend'))->isLoggedIn() ? 'Logged in' : 'Guest';