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] => Array ( [1] => 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?


  • Why are you accessing the session to determine the userid and their assigned role? This is a case where the Drupal APIs should suffice. global $user and $user->roles has the information you need.
    – mona lisa
    Dec 10, 2015 at 16:24
  • I came across some talk of $user but upon attempting to read that variable/object it doesn't exist, so I resorted to trying to pull a userid out of session data (a method I've used previously in another system, albeit not related to drupal, that sideloaded session data to check the userid).
    – AdamJS
    Dec 11, 2015 at 11:17
  • Ugh. Now I see what I did. I didn't realise I needed to call 'global $user' before attempting to access it. Thanks.
    – AdamJS
    Dec 11, 2015 at 11:20

1 Answer 1


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->uid;
$roles = $user->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.

  • Perfect, thanks. I had come across someone mentioning using $user but I didn't understand that I had to call 'global $user' in order to access it, so it didn't work out for me, which led me down the path of attempting to read the session data to get a userid with which to query the database (a method I've used before when I had to build some pages to sit alongside an existing installation of another (not drupal) system and dip into its database to grab things). Now I can start building this thing properly. Thanks.
    – AdamJS
    Dec 11, 2015 at 11:25
  • Sometimes we tend to leave implicit some details like that. :D
    – apaderno
    Dec 11, 2015 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.