1

In my module i have a php file which is called by an Ajax Post, after a user has clicked on a link. Now i need the customer-ID of that user who has clicked on this link.

    require_once("../../../../../Mage.php");
    umask(0);
    Mage::app();
    Mage::getSingleton('customer/session')->isLoggedIn()

Returns false at the same time at my phtml file with the same code it Returns true. I have tried other ways like

       Mage::getSingleton('core/session', array('name' => 'frontend'));
        if((Mage::getSingleton('customer/session')->isLoggedIn())) {
            $productPriceString = "true";
        } else {
            $productPriceString = "false";
        }

or even

    define('ROOT',Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB));
    $coreSession = Mage::getSingleton('core/session', array('name' => 'frontend'));

    if(isset($coreSession['visitor_data']['customer_id'])){
        $customerId = $coreSession['visitor_data']['customer_id'];
    } else {
        header('Location: '.ROOT.'customer/account/login/');
    }

    Mage::getSingleton('customer/session')->loginById($customerId);

    $customerSession = Mage::getSingleton("customer/session");
    if(!$customerSession->isLoggedIn()) {
        header('Location: '.ROOT.'customer/account/login/');
    }

But the result was always the same. So my question how can i get the id or even the _Customer object.

Thanks

EDIT

$session= Mage::getSingleton("core/session");

The result in my phtml file is object(Mage_Core_Model_Session)#125 (8) But the result in my external php file is object(Mage_Core_Model_Session)#145 (8)

3

I have runned this script and it's working. It provides me the customer id. Before you run it, delete the cache and session folders under you var folder

<?php
require_once ( "app/Mage.php" );
Mage::app();

Mage::getSingleton("core/session", array("name" => "frontend"));
$session = Mage::getSingleton("customer/session");

if($session->isLoggedIn())
{
    echo $session->getCustomer()->getId();
}else{
    echo "Not logged in";
}
  • 1
    Deleting var/session is never an appropiate solution. magento.stackexchange.com/questions/94589/… – Fabian Schmengler Sep 14 '16 at 11:17
  • This morning I've started checking my oldest answers. Soon, I discovered this answer and I labeled it as "I'm not proud of it". Definitely a bad idea to entirely remove the var/session. Thank you for the review :D. – Alex Constantinescu Mar 18 '17 at 8:16
0

try

require_once ( "/your-magento-directory/app/Mage.php" );
umask(0);

// Initialize Magento
Mage::app("default");

// You have two options here,
// "frontend" for frontend session or "adminhtml" for admin session
Mage::getSingleton("core/session", array("name" => "frontend"));
$isLoggedIn = Mage::getSingleton("customer/session")->isLoggedIn();
  • This does not work. Im logged in and the result is still false. – Philipe Reichert Oct 29 '15 at 8:08
0

I have used it one of my projects like this hope might help you

Customer Session :

<?php
// Include Magento application
require_once ( "app/Mage.php" );//path to your magento Mage.php file
umask(0);

// Initialize Magento
Mage::app();

// You have two options here,
// "frontend" for frontend session or "adminhtml" for admin session
Mage::getSingleton("core/session", array("name" => "frontend"));

$session = Mage::getSingleton("customer/session");

if($session->isLoggedIn())
{
    echo "Logged in";
}else{
    echo "Not logged in";
}

Magento Session from external page (same domain)

<?php
require_once ('app/Mage.php');
Mage::app(); 

// Define the path to the root of Magento installation.
define('ROOT', Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB));

// Obtain the general session and search for an item called 'customer_id'
$coreSession = Mage::getSingleton('core/session', array('name' => 'frontend'));
if(isset($coreSession['visitor_data']['customer_id'])){
    $customerId = $coreSession['visitor_data']['customer_id'];
} else {
    header('Location: '.ROOT.'customer/account/login/');
}

// Load the user session.
Mage::getSingleton('customer/session')->loginById($customerId);
$customerSession = Mage::getSingleton("customer/session");

// We verified that created successfully (not required)
if(!$customerSession->isLoggedIn()) {
    header('Location: '.ROOT.'customer/account/login/');
}

// Load customer
$customer = $customerSession->getCustomer();

// We get cell phone
$telefono = $customer->getTelefonoMovil();
?>
  • I tried your solution already in the past but this is not working for me :-( i have absolutely no idea why – Philipe Reichert Oct 29 '15 at 10:20
0

(this would be in a comment but not enough rep to comment at the time of writing)

The popular code snippet worked for me, but only when the file containing the snippet is in the same folder as the Magento install. I suspect( hope) there is a way to configure the base install dir in Magento to allow for doing this outside the mage root folder.

<?php
require_once ( "app/Mage.php" );
Mage::app();

Mage::getSingleton("core/session", array("name" => "frontend"));
$session = Mage::getSingleton("customer/session");

if($session->isLoggedIn())
{
    echo $session->getCustomer()->getId();
}else{
    echo "Not logged in";

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.