欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

how to save temporary session variables 保存到session

shiping1 的头像

how to save temporary session variables

How to I define and use session variables in drupal so that I can store temporary information while browsing through pages? I would be very thankful for any help.

Comments

Sessions are started in session.inc which is loaded by boostrap.inc which is almost the first thing done in index.php. You should be able to access session variables everywhere in your code using the $_SESSION array. If you are building theme xyz then set up the following code.

<?php
if(!isset($_SESSION['xyz']))
    {
   
$_SESSION['xyz'] = array();
    }

$xyz = & $_SESSION['xyz'];
?>

When you want to store variable z from page to page you can use the following code.

<?php
$xyz
['y'] = $y;
?>

On the next page you can access $y using the following code.

<?php
$y
= & $xyz['y'];
?>

Are you experienced at using & to create references?

http://petermoulding.com/technology/content_management_systems/drupal/

Thanks to peterx for describing this function! It was exactly what I needed, and only took 5 pages of search to find it! A good one for the handbook.

$_SESSION['xyz'] = array();

Thx peterx. Note the addition of an array to the session global. You cannot use scalars, as they won't be recognized (or stored) by Drupal, and you'll scratch your head wondering why your session variable isn't getting updated. E.g. this won't work:

$_SESSION['xyz'] = 'fred';


来自 https://drupal.org/node/33513

 

普通分类: