How to Use Sessions in Magneto?

Session is a way to preserve certain data across subsequent accesses.

Session enables you to build more customized applications and increase the appeal of your Magento store.

Here we will see how to use sessions in Magento.

Set Session Variable

Syntax

Mage::getSingleton('core/session')->set<SESSION_NAME>(<VARIABLE_NAME or VALUE>);

Example

<?php
 $emailId = '[email protected]';
  // Set value in session
  Mage::getSingleton('core/session')->setEmailID($emailId);
?>

    

Get Session Variable

Syntax

Mage::getSingleton('core/session')->get<SESSION_NAME>();

Example    

<?php
// Get session value
 $emailIdValue = Mage::getSingleton('core/session')->getEmailID();
?>

Unset Session Variable

Syntax

Mage::getSingleton('core/session')->uns<SESSION_NAME>();

Example

<?php
// Unset session value
Mage::getSingleton('core/session')->unsEmailID();
?>

Yo can use different sessions in frontend and backend as below.

In Frontend

Core Session : Mage::getSingleton('core/session')

Customer Session : Mage::getSingleton('customer/session')

In Backend

Core Session: Mage::getSingleton('adminhtml/session')

With the help of above code you can use sessions In Magento easily.

If you face any problems or need our professional services, please email us at [email protected]

 

Join our Newsletter

Never miss the latest offers, voucher codes and useful articles delivered weekly in your inbox.