Magento Sesssion

Clear entire session
Mage::getSingleton(‘checkout/session’)->clear();


// Magento uses different sessions for 'frontend' and 'adminhtml'Mage::getSingleton('core/session', array('name'=>'frontend'));
Mage::getSingleton('customer/session')->isLoggedIn();

$myData Mage::getSingleton('customer/session')->isLoggedIn();
echo $myData;

$session = Mage::getSingleton("core/session", array("name"=>"frontend"));
// set data
$session->setData("device_id", 4);
// get data
$myDeviceId = $session->getData("device_id");

Core Session:- Mage::getSingleton(‘core/session’)
Customer Session:- Mage::getSingleton(‘customer/session’)
Admin Session:- Mage::getSingleton(‘adminhtml/session’)

By looking at Mage_Core_Model_Session_Abstract class in Magento, you can easily set, retrieve, and unset session variables. So if you’re planning to code related in session, use the built in code of magento instead of hardcoding it. Here are some examples:

1. To set Magento Session
$yourSessionValue = 'session Value'; 
Mage:getSingleTon('core/session')->setSessionName($yourSessionValue);

2. To retrieve Session Variables
Mage:getSingleTon('core/session')->getSessionName();

3. To unset
Mage::getSingleton('core/session')->unsSessionName();

Important: Please note that "SessionName" in unsSessionName, getSessionName, and setSessionName can be any text you want / or simply it refers to the name of the session variable. However, the prefixes 'uns', 'set', and 'get' are required.  


Es wird eine individuelle Variable mit den Namen “magentoTest” angelegt. Der Wert für diese ist in diesem Fall “hello magento”.
?
1
2
$session = Mage::getSingleton('core/session'array('name'=>'frontend'));
$session->setMagentoTest('hello magento');

Abfragen der Werte:

?
1
$test = Mage::getSingleton('core/session')->getMagentoTest();

Session Variable löschen / unset

?
1
Mage::getSingleton('core/session')->setMagentoTest();

Nur eine bestimmte Session-Variable löschen:
?
1
Mage::getSingleton('core/session')->unsMagentoTest();

(Im Frontend die Customer oder Core Session benutzen. Im Backend die Adminhtml Session benutzen.)
Core Session:- Mage::getSingleton(‘core/session’)
Customer Session:- Mage::getSingleton(‘customer/session’)
Admin Session:- Mage::getSingleton(‘adminhtml/session’)

Use of session in Magento:

Use of session in Magento:
Let’s start this blog with the session definition, ‘It is a way to preserve certain data across subsequent accesses’. This enables you to build more customized applications and increase the appeal of your web site. Now, Let’s see how to set ,unset and retrieve a session in Magento. 
Generally, Magento session is handled by core Module ‘core/session’. We use “set<session name>” to set a session variable. For more understanding, look on to the following syntax and example.
Mage::getSingleton(‘core/session’)->set<YOUR SESSION NAME>(<VARIABLE NAME>);
Example :
$myValue =’test’;
Mage::getSingleton(‘core/session’)->setMyValue($myValue);In this example, I have declared the variable $myValueas “test”. I am going to set this variable in session. To do so, I have used the default syntax of the Magento in line 2. Now the session variable is set in the magento.

Wow, its easy to set the session variable.
Let’s now look, how to get value from the session variable “MyValue”.  To retrieve the  session variable, we use “get<YOUR SESSION NAME>();” .
Mage::getSingleton(‘core/session’)->get<YOUR SESSION NAME>();In our example, we need to get the session value of “MyValue”. For this, let’s use “getMyValue();”. So our code will be as follows,

$getSession =Mage::getSingleton(‘core/session’)->getMyValue();
Next, we will see how to unset the session variable. To unset the session variable, we have the following syntax,
Mage::getSingleton(‘core/session’)->uns<YOUR SESSION NAME>();Let’s have a simple example to unset the session variable.

Mage::getSingleton(‘core/session’)->unsMyValue();
This code will automatically delete the session of the MyValue.
How to Use customer or core session in frontend. Use adminhtml session in the backend.
Core Session :- Mage::getSingleton(‘core/session’)
Customer Session :- Mage::getSingleton(‘customer/session’)
Admin Session :- Mage::getSingleton(‘adminhtml/session’)
Shopping Cart Session :-Mage::getSingleton(‘checkout/session’)->getQuote()

How to Set, Retrieve and Unset Magento Session Variables

To set a Magento session variable:
  • $myValue = 'Hello World';
    Mage::getSingleton('core/session')->setMyValue($myValue);
To Retrieve:
  • $myValue = '';
    $myValue=Mage::getSingleton('core/session')->getMyValue();
To Unset:
  • Mage::getSingleton('core/session')->unsMyValue();


$SESSION_VARIABLE = '';
$SESSION_VARIABLE=Mage::getSingleton('core/session')->getSESSION_VARIABLE_NAME();

Magento session management (Core/Checkout/Customer)

Today we are going to cover magento session management. Magento has created session models in the following four areas or sections of the website.
  • Core – this can be accessed using Mage::getModel(“core/session”) object. In this you can store global session data which you can use through out the site
  • Customer – this can be accessed using Mage::getModel(“customer/session”) object. In this you can store customer related session data which you can use related to customer areas of your website like account, login, forgotten password pages etc.
  • Checkout – this can be accessed using Mage::getModel(“checkout/session”) object. In this you can store checkout related session data which you can use related to checkout areas of your website like basket, one page checkout pages etc.
  • Admin – this can be accessed using Mage::getModel(“admin/session”) object. In this you can store admin related session data which you can use anywhere in the admin areas of your website.
The above classes have been created by Magento so that you can create same session variables with same name for different areas of the site.

Let’s have a look at the examples below -:
Here we are storing the different data in the same session name variable -:
Mage::getModel('core/session')->setData('foo','global data');
Mage::getModel('customer/session')->setData('foo','customer data');
Mage::getModel('checkout/session')->setData('foo','checkout data');
Mage::getModel('admin/session')->setData('foo','admin data');
Mage::getModel('your_custom_module/session')->setData('foo','custom data');
Here we are accessing the different data from the same session variable -:
Mage::getModel('core/session')->getData('foo'); 
Mage::getModel('customer/session')->getData('foo');
Mage::getModel('checkout/session')->getData('foo'); 
Mage::getModel('admin/session')->getData('foo'); 
Mage::getModel('your_custom_module/session')->getData('foo');
This way you have very less likely to having conflicts with other modules which are using session variables. The best way is to create your own session class if your module is highly using session variables otherwise you can use session variable name as unique as possible like namespace_modulename_foo to avoid conflicts.
How can you create your own session class?
If you try all the above model classes they all are extended by Mage_Core_Model_Session_Abstract class and in the same manner you can create your own session model class by extending the abstract class.
The key is to call the init() function of abstract class to register unique namespacing of your session class. So if you look at this class Mage_Checkout_Model_Session, it calls the init() function‘checkout’ as argument.
public function __construct()
{
    $this->init('checkout');
}
The above code actually represents $_SESSION['checkout']and when you do the following
Mage::getModel("checkout/session")->setFoo('some value for checkout data')
It represents $_SESSION['checkout']['foo'] =’some value for checkout data’;
In the same manner you can create you own session class Scommerce_Custom_Model_Session
class Scommerce_Custom_Model_Session extends Mage_Core_Model_Session_Abstract
{
    public function __construct()
    {
        $this->init('custom_unique_name');
    }
}
And store and access the session variable as follows -:
$custom = Mage::getModel('custom_unique_name/session');
$custom->setFoo('Some Value');
$custom->getFoo('Some Value');
Hope this article helped you in some way. Please leave us your comment and let us know what do you think?

Mage::getSingleton( 'customer/session' )->setData( 'yourArray', array( 1, 2, 3 ) );

Magento session: 
Magento session is handle by core Module ‘core/session’. Here is the way to save Session in magento

$session = Mage::getSingleton('core/session');


$session->setData('my_magento_session'array('id' => "my_session_id_value"'setAt' => time()));

This will store a array of session “my_magento_session”.
we can get store session like this

$mysession = Mage::getSingleton('core/session')->getData('my_magento_session');


  1. //Show Error Message in frontend  
  2. $message = $this->__(Error accured!.’);  
  3. Mage::getSingleton(‘core/session’)->addError($message);   
  4. $message = $this->__(Data saved Successfully’);  
  5. Mage::getSingleton(‘core/session’)->addSuccess($message);  
  6.   
  7. //Show Error Message in Admin page  
  8. $message = $this->__(‘Error accured’);  
  9. Mage::getSingleton(‘adminhtml/session’)->addSuccess($message);   
  10.   
  11. //Show Success Message in Admin page  
  12. $message = $this->__(‘Data saved Successfully’);  
  13. Mage::getSingleton(adminhtml/session’)->addError($message);  
  14.   
  15. //You can create new session using below syntax:  
  16. Mage::getSingleton(‘core/session’)->setYourVariable(‘data’);  
  17. $Data = Mage::getSingleton(‘core/session’)->getYourVariable();  
  18.   
  19. //Create session variable for single time access only.  
  20. Mage::getSingleton(‘customer/session’)->setData(‘message’array(‘success’ => ‘Record updated successfully’));  
  21.   
  22. //How to get in PHTML file session variable.  
  23. $messages = Mage::getSingleton(‘customer/session’)->getData(‘message’, true);  

$sessionId $mysession["id"];

and session can be removed with setting session variable null