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.
1 | Mage::getSingleton('core/session')->setMyCustomData('blah'); |
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
1 2 | $session = Mage::getSingleton('core/session', array('name' => 'frontend'));
echo $session->getMyCustomData(); |
10 Comments
Leave a comment
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (1)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)
Useful Links
Tag Cloud
500 internal server error admin answers attribute bug category checkbox checkout cookie customer difference domain name EAV error event extension interview invoice jquery linux magento magento2 magento admin magento error magento interview questions magento orm mysql observer order pinterest product products questions redirect register remove script session simplexml to array state status study guide tax url wordpress
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?