In one of my drupal site (Drupal 4.4 and php 4.3.8), session variables are stored. But in my other site (Drupal 4.4 and php 4.1.2), they are not. Both are running on linux (former is on red hat and the latter is on debian).

But I guess this is not because of php version because I tried to create a script (outside Drupal) and run it against both sites, and it did stored session variables.

Im thinking, is this because of php settings? Or the different version of linux.

Can someone have an idea? Please help me. Thanks...

Comments

Jhef.Vicedo’s picture

But how these session variables saved into database? And how to retrieve them?

And can I use $_SESSION in #drupal?

jhefmv [at] gmail [dot] com
Work smarter, not harder!

Dries’s picture

The session lifetime is a PHP setting, not a Drupal setting. Drupal sets sensible defaults through the .htaccess file (but you can also choose to modify your PHP's php.ini in case your .htaccess file is not being parsed).

Jhef.Vicedo’s picture

Does this mean, I can use $_SESSION variables and set some values into it but after the next page reload, it will automatically destroyed?

I think my server was able to parsed my .htaccess file. But, where is this session lifetime set, I mean what directive is it? Isn't it included in the default settings?

jhefmv [at] gmail [dot] com
Work smarter, not harder!

Dries’s picture

You can use $_SESSION variables. They will be 'destroyed' after the session expired (not likely after the next page reload).

Jhef.Vicedo’s picture

So most likely, there is a wrong configuration in my php settings that causes my $_SESSION variables to be destroyed? Because I tried to print_r($_SESSION) in my theme_page() function, and every time a page is loaded, the output is Array(). Except only to the page where I set some values onto $_SESSION variables.

jhefmv [at] gmail [dot] com
Work smarter, not harder!

Anonymous’s picture

---

OliW’s picture

I've got the same sort of thing on 4.7

What I'm trying to do is include a .php file that sets a $_SESSION into the output of a node. The include works but if I try and access the session var from another page it's already dead. It seems they're being dropped striaght after the request -- and that's mighty annoying.

Even if there's not a proper fix for this, is there at least a workaround?

Oli Warner
KittenAuth.com

StevenPatz’s picture

I set a session variable, and when I go to read that saved variable it's always blank.

CyberKate’s picture

Hi !

I also have the same problem...

I've created new PHP page in Drupal like this :
- a variable of session ;
- a link to an application on another server.

If I want to use the variable of session that I've created on Drupal, I can because it doesn't exists anymore on the other server.

Why not ?

audrey@vancouver.php.net’s picture

I was having the same problem, for storing session data for anonymous users.
For authenticated users, no problem, to store temporary session variables.

For an anonymous user, when a variable is set to $_SESSION, this variable gets stored in the database sessions table. However, on the next page, you navigate to, the $_SESSION was not being read from the database, and at the end of that page, the empty $_SESSION data would be written to the sessions table, removing the temporary variable that had been saved in the sesssions table.

Finally tracked it down to the includes/session.inc file, the sess_read() function

  // retrieve data for a $user object
  $result = db_query("SELECT sid FROM {sessions} WHERE sid = '%s'", $key);
  if (!db_num_rows($result)) {
    $result = db_query("SELECT u.* FROM {users} u WHERE u.uid = 0");
  }
  else {
    $result = db_query("SELECT u.*, s.* FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.sid = '%s'", $key);
  }

The query inside the else statement is executed for an anonymous user, when a session is started. However, because of the inner join, when there is a row in the sessions table, but no row from the user table, this returns an empty result set.

I modified the 'INNER JOIN' to a 'RIGHT JOIN' so that there will be a result set, when there is a row in the sessions table without a row in the users table,

If anyone knows another way, e.g. adding a module, instead of hacking the core, please let me know.

telcontar’s picture

This does not seem to be an issue as of Drupal 5.x.

The 'users' table has a row with uid 0 for anonymous users, so no outer join is necessary. Not sure whether upgrading to 5.x is a possibility, but I strongly recommend it!

-- telcontar

keff’s picture

You can read about why user with uid=0 disappeared from your site here: http://drupal.org/node/243423
To fix, just find the user with blank name and password in 'users' table and change its uid back to 0 - no need to rewrite SQL.

javierlandini’s picture

I spent 8 hours trying to find out why I could't store session data if I was not logged in!!!! And I had other Drupal sites where I had session data stored with no problem for anonymous users! I was going crazy. Thanks a lot again, and I also think that this would be someway better documented and Drupal should warn you if there is no anonymous user in the database.