I am using Magento and trying to save a value in the session as follows in its index.php file, but the value is not being retained.
$_SESSION['myvar'] = '1';
How do I do it?
Thanks
Session variables are stored in associative array called $_SESSION[]. These variables can be accessed during lifetime of a session. The following example starts a session then register a variable called counter that is incremented each time the page is visited during the session.
You need to call session_start before you check if $_SESSION['can'] has a value. You also do not need to destroy and create a new session just to change a value. <? php session_start(); if (isset($_SESSION['can'])) { $_SESSION['can'] = 2; } else { $_SESSION['can'] = 1; } header('Location: '.
Session variables are a way to store data about a user in a database and retrieve it later. Cookies are a way to store data about a user on the user's computer. Session variables are typically used in applications that need to keep track of a user's activity.
Let's say you want to save the value "Hello world" to the "welcome message" variable in the session. The code would be :
$inputMessage = 'Hello World'; Mage::getSingleton('core/session')->setWelcomeMessage($inputMessage);
Now you want to echo the "welcome message" somewhere else in your code/site.
$outputMessage = Mage::getSingleton('core/session')->getWelcomeMessage(); echo $this->__($outputMessage);
Following the example given by Ali Nasrullah, I would do:
$session = Mage::getSingleton("core/session", array("name"=>"frontend")); // set data $session->setData("device_id", 4); // get data $myDeviceId = $session->getData("device_id");
Make sure you include [Mage-root]/app/Mage.php befor calling the code above!
@Ali Nasrullah: Pass the value of device:id as second parameter of the setData function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With