Create your own session namespace for storing your custom session variable


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");</div>
<div>
]
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 (X11; Ubuntu; Linux i686; rv:17.0) Gecko/17.0 Firefox/17.0
)[session_hosts] => Array
(
[192.168.1.100] => 1
)

[foo] => bar
)

)

Leave a comment