Jun 19, 2011

Magento: Get and set variables in session

Suppose you want to set data in the magento session, so that you can retrieve it from other pages. Session has been very useful global variable in PHP and using it we can easily retrieve and add data to it and get it from the entire site.

Here is the code that will set / add your custom data into session.


here, setMyCustomData can be replaced with the meaningful name as per your need. One thing to be cautious while setting session variables is to make sure it doesn’t conflict with magento’s internal variables, example setData.

Code to retrieve the session variable

$session = Mage::getSingleton('core/session', array('name' => 'frontend'));
echo $session->getMyCustomData();


  • This is just now the sort of information I was looking for! Thanks.

  • Great! thanks for the share!

  • Hello,
    How and where exactly do I put this code? I am new to magento and need some help where I put the code

    • Anywhere as long as it’s in Magento’s PHP or PHTML file. First set it in some file, and then you can get it anywhere in Magento frontend scope.

      • Not working with PHTML as I need to flush cache for the new session otherwise it displays the value of the previously set session.

  • hi

    i want to use simple php session into magento phtml file how i can do it

    • you can use above method to store session in magento.

  • how do i can set session in index.php file in magento

  • Thanks this is working fine and i did my work done

  • Hi can i append session variable in URL?

Leave a comment


Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!




Magento top 50 contributors

Magento top 50 contributors