0

I was trying to acquire session of logged in customer in one of external PHP script in the same folder as my Magento installation:

<?php

require_once 'app/Mage.php';
umask(0);
Mage::app();

$session = Mage::getSingleton('customer/session', array('name' => 'frontend'));
var_dump($session->getId());
var_dump($session->getCustomerId());

$coreSession = Mage::getSingleton('core/session', array('name' => 'frontend'));
if(isset($coreSession['visitor_data']['customer_id'])){
    $customerId = $coreSession['visitor_data']['customer_id'];
} else {
    die('Action not allowed. (1)');
}

Which output when accessed via the exact domain as my store:

NULL NULL Action not allowed. (1)

When I'm logged in my store frontend as a test customer.

What am I doing wrong here?

1 Answer 1

0

It's weird but may be because some security patch or something that changed the session behaviors of Magento. This works now:

<?php

require_once 'app/Mage.php';
umask(0);
Mage::app();

// Add this line before everything
$coreSessionFront = Mage::getSingleton('core/session', array('name' => 'frontend'));

// This works now
$session = Mage::getSingleton('customer/session', array('name' => 'frontend'));
var_dump($session->getId());
var_dump($session->getCustomerId());

// This won't work
//$coreSession = Mage::getSingleton('core/session', array('name' => 'frontend'));
//if(isset($coreSession['visitor_data']['customer_id'])){
//    $customerId = $coreSession['visitor_data']['customer_id'];
//} else {
//    die('Action not allowed. (1)');
//}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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