global $user

You are here

  1. 7 developer/globals.php user
  2. 6 developer/globals.php user
  3. 8 developer/globals.php user

An object representing the user currently visiting the site.

Contains preferences and other account information for logged-in users. When a user is not logged-in, the $user->uid property is set to 0.

File

developer/globals.php, line 85
These are the global variables that Drupal uses.

Code

global $user;

Comments

The global user object contains some basic data from Drupal core. It does not automatically include data from other modules, including the core profile module. To get all data in the user object, you need to do a full user_load(). The following is a first attempt at documenting the information that core puts into the object. Items with (?) are where I didn't know and took my best guess. If anyone knows more on this and can fill in the blanks, please leave a comment. I will at some point gather all the comments together into one and then file an issue to get it added to the official documentation.

  • uid - User ID
  • name - User name
  • pass - Encrypted password
  • mail - User email address
  • theme - (?) Name of the theme shown for this user (no longer changeable in core but can be in contrib)
  • signature - Signature as set in the user account settings
  • signature_format - Text format to apply to signature
  • created - Unix timstamp for when the account was created
  • access - Unix timstamp of the last time the user accessed the site
  • login - (?) Unix timstamp of the last time the user logged in
  • status - 1 if the user is active, 0 if blocked
  • timezone - User's timezone, if user selectable timezones are enabled
  • language - User's language code
  • picture - User picture / avatar
  • init - (?) Contains the user's email
  • data - (?)
  • sid - (?) Session ID
  • ssid - (?)
  • hostname - User's IP address
  • timestamp - (?)
  • cache - (?)
  • session - (?) Variables stored in the session
  • roles - Array of roles assigned to the user

From the 'users' table:

uid - User ID
name - User name
pass - Encrypted password
mail - User current email address
theme - Name of the theme shown for this user (no longer changeable in core but can be in contrib)
signature - Signature as set in the user account settings
signature_format - Text format to apply to signature
created - Unix timstamp for when the account was created
access - Unix timstamp of the last time the user accessed the site
login - Unix timstamp of the last successful login by this user
status - 1 if the user is active, 0 if blocked
timezone - User's timezone as a PHP compatible timezone string ( date_default_timezone_set() )
language - User's language code
picture - User picture / avatar
init - Contains the email address the user provided during initial registration
data - Data stored in the users table by contrib modules (second argument of user_save())

From the 'sessions' table:

sid - Session ID for HTTP sessions
ssid - Session ID for HTTPS sessions
hostname - User's IP address
timestamp - A timestamp of last user access
cache - A timestamp used in cache.inc to check freshness of cached data
session - Variables stored in the session through $_SESSION

From the 'user_roles' table:
roles - Array of roles assigned to the user

ssid - (?) Single Sign-on ID

I am taking a guess.

Secure session ID

If this is any help, the data for the global user object comes from these tables
users
sessions
roles
user_roles
If you check the field description of the above, it will unscramble the above object for you fairly easily.

if ($user->uid == 0) {
  //user is not logged in
}

if working with multiple rolls, access them this way

$user->roles[0] admin
$user->roles[1] anonymous user
$user->roles[2] authenticated user1
$user->roles[3] authenticated user2

i wana use user picture with user name in drupal 7,if any one have any solution plz send me....thanks

I would like to ask how to set uid by ourselves when we great new account?

The user ID is owned by the database.
So ... if you dynamically create a user...

$newuser = new stdClass();
$newuser->name = 'johnny';

and save it: user_save($newuser);
it will return a brand-spankin'-new user object, or FALSE if it couldn't save it.

I wouldn't try to manually set the $user->uid because Drupal handles it for you.

Actually, you want this.

$newuser = user_save(drupal_anonymous_user(), array('name'=>'johnny'));

Much more compact. However, there's a todo on user_save() saying they're going to change the behavior, so you might do something like this:

$newuser = drupal_anonymous_user();
$newuser->name = 'johnny';
$newuser = user_save($newuser);

There are 22 sequences installed by drupal 7.17 in my PostgreSQL database. Which sequence is used by the "users" table?

The default value of users.uid is 0!
instead of something like: nextval('sequence_name')

I am currently using this to detect if a user just logged in:

if (property_exists($GLOBALS['user'],'login') && $GLOBALS['user']->login == $GLOBALS['user']->timestamp) {
//Do stuff
}

It seems to work correctly. Is there any situation, where this will be incorrect.

I want to filter role for users profile page from owner of the profile page not from who logged in?

how to creating programmatically new role like anonymous

For Drupal 8 use:

$account = \Drupal::currentUser();

instead of this global.

Reference: https://drupal.org/node/2032447