Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Create your own session namespace for storing your custom session variable
 
zaheerabbasbadi
Jr. Member
 
Total Posts:  28
Joined:  2012-05-16
 

First, make sure you have your own custom module if not then create.

and make session.php file in model folder of your module and write down below code in it.

<?php
class <namespace>_<module>_Model_Session extends Mage_Core_Model_Session_Abstract 
{
 
    
public function __construct() {
        
        $namespace 
'mysession';
         
        
$this->init($namespace);
        
Mage::dispatchEvent('myapp_session_init', array('myapp_session' => $this));
    
}
 
}

and then now you can store session variable like

// Set the "foo" variable
Mage::getSingleton("myapp/session")->setFoo("bar");
// Same thing
Mage::getSingleton("myapp/session")->setData("foo""bar");

// Another example. Let's say we want to name our variable "foo_bar"
Mage::getSingleton("myapp/session")->setFooBar("data");
// or
Mage::getSingleton("myapp/session")->setData("foo_bar""bar");

// Get data the same way
Mage::getSingleton("myapp/session")->getFoo()
// or
Mage::getSingleton("myapp/session")->getData("foo");

you can see output if you print array

[Mysession] => Array
        (
            
[_session_validator_data] => Array
                (
                    
[remote_addr] => 192.168.1.100
                    [http_via] 
=> 
                    
[http_x_forwarded_for] => 
                    
[http_user_agent] => Mozilla/5.0 (X11UbuntuLinux i686rv:17.0Gecko/17.0 Firefox/17.0
                
)

            
[session_hosts] => Array
                (
                    
[192.168.1.100] => 1
                
)

            
[foo] => bar
        
)

)
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top