1

I'm using Mangeto 1.9.1

I'm trying to access the magento customer session outside magento in a php file in the root folder of the magento.

Here is my code:

<?php
error_reporting(E_ALL | E_STRICT);
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
$app = Mage::app('default'); 
Mage::app();

$customer_id = Mage::getSingleton('customer/session')->getId();


$GrandTotal = Mage::getModel('sales/quote')->loadByCustomer($customer_id)->getGrandTotal();
$HalfTotal = $GrandTotal / 2;
echo "Grand: $GrandTotal<br>Half: $HalfTotal";
?>  

My magento is installed on a subdomain like beta.mymagento.com

But it seems it can not access the session. How i can fix it?

Thanks in advance!

user2942786
  • 123
  • 2
  • 14

1 Answers1

3

You're having an issue picking up the correct session because Magento stores its session ID in a different cookie than typical PHP apps. You need to first initialize a core session, and tell it to look at a cookie named frontend instead of whatever PHP defaults to. Your code should look like the following:

...

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

...

Also, you most likely only need one call to Mage::app().

Note: if you have multiple stores/websites in your Magento install, you will have to call Mage::app() with the appropriate identifiers, e.g. Mage::app('<website_code>', 'website');.

Franklin P Strube
  • 2,215
  • 18
  • 12