Under Drupal 5.x $_SESSION variables are not supported . If you try to show or hide a block using:

if ( $_SESSION['this_block_enabled'] == true )
{
   return TRUE;
}
return FALSE;

It never seems to work, despite the fact that the $_SESSION variable is set, which you can verify by inspecting through debugging.

I didn't think Drupal was this limitted - and I certainly don't want to open drupal core code to make this work through a hack. This is a major set back on Drupal's quality and flexibility.

Comments

heine’s picture

This behaviour doesn't make any sense. Can you please post the full code? Where do you use the code? Does this happen for registered users, anonymous users, both?

--
The Manual | Troubleshooting FAQ | Tips for posting | How to report a security issue.

shija03’s picture

Basically I am using CCK and computed fields to create my own content type. I have blocks on the "left-sidebar" that need to be hidden at times, and at other times they will need to appear (this goes for anonymous and registered users).

I have a series of Nodes that when they are invoked, they will set the variable $_SESSION['section_enabled'] to a specific value. So if I have a node that sets $_SESSION['section_enabled'] = 'menu1', and other Nodes set the variable to 'menu2', and 'menu3'.

There are 3 left-sidebar blocks, which happen to be menus, and they are displayed by running the following code:

BLOCK MENU1 is displayed when

   if ( $_SESSION['section_enabled'] == 'menu1' )
      return TRUE;
   return FALSE;

BLOCK MENU2 is displayed when

   if ( $_SESSION['section_enabled'] == 'menu2' )
      return TRUE;
   return FALSE;

BLOCK MENU3 is displayed when

   if ( $_SESSION['section_enabled'] == 'menu3' )
      return TRUE;
   return FALSE;

The bottom line is that this does not work at all times, sometimes (about 10% of the times), by some unknown circumstance, the block will appear - but 90% of the times I don't see this happening.

Perhaps someone has encountered and resolved this in an elegant way.

It would also be nice if we could incldue the <img /> tag so we could at least display images that could easily illustrate the problems that we are encountering.

gpk’s picture

1. Could this be a server issue?
- Try on a different hosting account, if you have one
- Or try on a localhost installation http://drupal.org/node/157602. I use XAMPP for Windows.

2. For basic troubleshooting,
- you can use drupal_set_message() to print out messages etc.,
e.g. try drupal_set_message("Section enabled: $_SESSION['section_enabled']"); at various places.
- You should also see session data being stored in table {sessions} in the DB, serialized.

Hope this helps with debugging ...

Re. images, the best you can do is to host them elsewhere and link to them from here :-(

Another thought - if you only want your menus 1, 2 and 3 to be displayed when a page from the relevant section is showing then you could use taxonomy to tag the pages 'menu1' etc. and then use http://api.drupal.org/api/function/taxonomy_node_get_terms_by_vocabulary/5 to get find out which menu block should be displayed. Maybe this will help, I don't know. But it won't help if you want the menu to be "remembered", unless you store the info in the session which gets us back to the original problem :-P

gpk
----
www.alexoria.co.uk

sdgreene’s picture

I've had similar experiences myself. I've had mixed results using superglobals.
You might try adding global $_SESSION; at the top of the relevant code block.

gpk’s picture

I use it in custom modules to preserve session-related info. between one page view and the next.

gpk
----
www.alexoria.co.uk

moksa’s picture

http://drupal.org/node/204397
http://drupal.org/node/125947
http://drupal.org/node/59483

I read these thread and still no response... (php5 apache2 drupal 5.8)

my drupal is on myweb.com/drupal/

in myweb.com/test.php i've :

session_start();
$_SESSION['truc'] = "chickchik";

session is set without problem i can read it on another test2.php.

if y put this code in a drupal page content (myweb.com/drupal/node/11) :

session_start();
echo $_SESSION['truc'];

nothing is printed.

What i've forgot ?

thanks in advance !
++

moksa’s picture

Ok $_SESSION are in user class $user->session

:)

gpk’s picture

Note:
- if you want to use a Drupal session variable in your own script then you need to do a Drupal bootstrap first (see the first 2 lines of index.php)
- code run in a drupal PHP page doesn't need to session_start() - that happened during Drupal bootstrap.

gpk
----
www.alexoria.co.uk