Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a session variable in drupal 7?

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.

like image 877
user785975 Avatar asked May 07 '12 22:05

user785975


3 Answers

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:

lists_session("s_key", "value");

And to retrieve the value, just use:

$myVar = lists_session("s_key");
like image 63
Muhammad Reda Avatar answered Oct 23 '22 20:10

Muhammad Reda


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['mydata'] = array(of your data);
like image 43
Kristoffer Avatar answered Oct 23 '22 20:10

Kristoffer


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

like image 31
mana Avatar answered Oct 23 '22 18:10

mana