Odd $_SESSION contents I have a page in Drupal from which I want to be able to read the $_SESSION data to determine the userid and their assigned role. As an exploration I attempted to print to the page the contents of $_SESSION so that I could see what was being stored and what the keys were to retrieve these values.

When I attempt to print to the page using echo print_r($_SESSION);, instead of a set of reasonable session data, I instead get only this:

Array ( [batches] =&gt; Array ( [1] =&gt; 1 ) )

I had a bit of a Google around to see if I was doing something wrong, and I came across the documentation for session.inc, which says:


  These functions should not be called directly. Session data should instead be accessed via the $_SESSION superglobal.


So it seems that, while Drupal wants me to use $_SESSION to retrieve the session data, I can't do this because my $_SESSION data has only this odd pair of nested one-element arrays that store only the number 1.

I also came across some reports that not having the uid 0 in your users table can cause similar issues, so I have checked the users table and I do indeed have user 0.

Any ideas what is going wrong here?

Thanks.
The $_SESSION variable contains values set by Drupal; in your case, $_SESSION['batches'] is the value set by the Batch API. Drupal, although, doesn't use the session to store the user ID; that value is stored in the $user global variable, as cilefen correctly said in his comment.

global $user;
$uid = $user-&gt;uid;
$roles = $user-&gt;roles;


Keep in mind that the $user global variable doesn't contain a fully-loaded user object; to get that, you need to use the following code.

$account = user_uid_optional_load();


$account will contain every field associated with the user entity, and any property attached to the user object from third-party modules.