php 如何在drupal 7中保存会话变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10490253/
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-24 22:18:56  来源:igfitidea点击:

How to save a session variable in drupal 7?

phpdrupaldrupal-7session-variables

提问by user785975

i would like use a variable session ($_session) but it doesn't work in Drupal 7. What are the possibilities in Drupal 7 in order to save variable session.

我想使用可变会话($_session),但它在 Drupal 7 中不起作用。Drupal 7 中有哪些可能性可以保存可变会话。

回答by Muhammad Reda

You can try this.

你可以试试这个。

function lists_session($key, $value = NULL) {
  static $storage;
  if ($value) {
    $storage[$key] = $value ;
    $_SESSION['lists'][$key] = $value ;   // I use 'lists' in case some other module uses 'type' in $_SESSION
  }
  else if (empty($storage[$key]) && isset($_SESSION['lists'][$key])) {
    $storage[$key] = $_SESSION['lists'][$key];
  }
  return $storage[$key];
}

So, to save a variable in Session:

因此,要在 Session 中保存变量:

lists_session("s_key", "value");

And to retrieve the value, just use:

要检索值,只需使用:

$myVar = lists_session("s_key");

回答by Kristoffer

I have no problem of using the $_SESSION variable in my own module. Just keep in mind to create a unique sub key for your data.

我在自己的模块中使用 $_SESSION 变量没有问题。请记住为您的数据创建一个唯一的子键。

$_SESSION['mydata'] = array(of your data);

回答by fkaufusi

Remember to serialise your data such as array, obj... before save to session. $arr = array(); $_SESSION['mysession'] = serialise($arr);

请记住在保存到会话之前序列化您的数据,例如数组,obj...。$arr = 数组(); $_SESSION['mysession'] = serialise($arr);