简体   繁体   English

如何在Magento中保存我自己的会话变量中的值?

[英]How do I save value in my own session variable in Magento?

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. 我正在使用Magento并尝试在其index.php文件中按如下方式保存会话中的值,但该值未被保留。

$_SESSION['myvar'] = '1';

How do I do it? 我该怎么做?

Thanks 谢谢

Let's say you want to save the value "Hello world" to the "welcome message" variable in the session. 假设您要将值“Hello world”保存到会话中的“welcome message”变量中。 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: 按照Ali Nasrullah给出的例子,我会这样做:

$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! 确保在调用上面的代码时包含[Mage-root] /app/Mage.php!

@Ali Nasrullah: Pass the value of device:id as second parameter of the setData function. @Ali Nasrullah:传递device:id的值作为setData函数的第二个参数。

  Mage::getSingleton('core/session')->setMySessionVariable('MyValue'); 

  $myValue  =  Mage::getSingleton('core/session')->getMySessionVariable();

  echo $myValue;

 Take Look For More: 

Here are code to Get, Set, and Unset Session in Magento 以下是Magento中获取,设置和取消设置会话的代码

Here are code to Get, Set, and Unset Session in Magento 以下是Magento中获取,设置和取消设置会话的代码

frontend: Mage::getSingleton('core/session')->setYourNameSession($session_value);

backend: Mage::getSingleton('admin/session')->setYourNameSession($session_value);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM