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

时间:2021-10-19 07:22:07

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

谢谢

4 个解决方案

#1


74  

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”保存到会话中的“welcome message”变量中。代码是:

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

#2


33  

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

#3


11  

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

#4


7  

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

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

#1


74  

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”保存到会话中的“welcome message”变量中。代码是:

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

#2


33  

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

#3


11  

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

#4


7  

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

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