Call our sales line
08000 484 679
Home > Magento Tips > Adjusting the Scope of your Magento Session

Adjusting the Scope of your Magento Session

Posted on: 18th Dec 2012 By: Robert Kent 1 Comment

A long time ago I posted an article about extracting your Magento blocks outside of Magento. However many of you may have had an issue if you were combining a static/CMS website and Magento with your store sitting within a sub-folder.

Now what you might have noticed is that when your Magento session has started within say, http://www.mywebsite.com/shop/ and you try to echo a persistant cart in the header on your homepage (http://www.mywebsite.com) nothing shows up.

Well, the reason for this is that the Magento cookie path is incorrect. By default the cookie path is relevant to the Magento installation which means that if you use the following code to try to get your Magento session out on a lower (higher?) level of the website you’re going to run into problems.


require_once('shop/app/Mage.php');
$app = Mage::app(); // initialise our instance
$app->loadArea(Mage_Core_Model_App_Area::AREA_FRONTEND); // why not eh?
Mage::getSingleton('core/session', array('name'=>'frontend'));
echo $app->getLayout()->getBlockSingleton('checkout/cart_sidebar')->setTemplate("checkout/cart/sidebar.phtml")->toHtml();
// echo our minicart thing

You see if you’re in a level outside of your installation – the browser is not going to use your Magento cookies – and therefore you cannot grab your session information. To combat this all you need to do is ensure that your cookie path is set to the root of your website by logging into your Magento admin and navigating to System>Configuration>Web>Session Cookie Management:

Screen Shot 2012-12-18 at 09.08.28

Just putting a “/” in the cookies path should do the job!

Thanks for visiting our blog – and have a great Christmas!

By Robert Kent

Rob is Ecommerce Web Design’s lead PHP developer, and an expert at customising the Magento framework to create completely unique sites. Follow him on twitter (because he's a twit) http://twitter.com/kent_robert. - .

One Response to “ Adjusting the Scope of your Magento Session ”

  1. nz warrior
    #1 | 18th December 2012

    I have run into this issue before (I resolved mine by running mage in root folder) but will use this when the time comes to run magento in a subfolder.

    Great article Rob keep it up!

    Merry Xmas

    nz warrior

Post A Comment

Your comments:
Enclose code snippets within the appropriate tags: [php][/php]   [js][/js]   [xml][/xml]   [css][/css]   [html][/html]
E.g: [php]<?php echo "hello world"; ?>[/php]

Search Blog

Follow us on Twitter

Archives

For the record...

Views & opinions in this blog are those of the individual and do not necessarily reflect those of E-commerce Web Design or the Creare Group.