I'm building a module that uses the session id to store information. There does not seem to be a way to get the session id for anonymous users through any drupal API. I would have thought that the user object would hold the session id like it does for logged in users in $user->sid. After looking at the session.inc file, the $user object has the sid in the object from the query:

$user = db_fetch_object(db_query("SELECT u.*, s.* FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.sid = '%s'", $key));

but once it is realized that the user is anonymous, the sid is overwritten by $user = drupal_anonymous_user($session), no more $user->sid.

The only way that I've figured out how to get the session id out of drupal is from the cookie using $_COOKIE[session_name()], it seems the session id should be included in the $user object for anonymous users within drupal. Does drupal_anonymous_user() need to be refactored?

Comments

Wesley Tanaka’s picture

Does session_id() do what you want?

amarbijay’s picture

Hi,

The same problem we faced and also the issue is resolved, please see the solution for it.
First add Drupal message by using drupal_set_message('none') function
Then create your session value and assign value to it.
From template file try to hide this created message 'none';
In next page try to print the session variable, it will work fine.