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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3340982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:24:18  来源:igfitidea点击:

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

phpmagentosession

提问by Ali

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

谢谢

回答by Hervé Guétin

Let's say you want to save the value "Hello world" to the "welcome message" variable in the session. The code would be :

假设您想将值“Hello world”保存到会话中的“欢迎消息”变量中。代码将是:

$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);

回答by NiBa

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 函数的第二个参数传递。

回答by Jyotiranjan

  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 中获取、设置和取消设置会话的代码

回答by Ansyori

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

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