How to save session variable in drupal 7?

I would like to use a session variable ($ _session), but it does not work in Drupal 7. What are the possibilities in Drupal 7 for saving a session of a variable.

+5
source share
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 the variable in the session:

lists_session("s_key", "value");

And to get the value, just use:

$myVar = lists_session("s_key");
+11
source

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

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

, , obj... . $ arr = array(); $ _SESSION ['mysession'] = serialise ($ arr);

+1

All Articles