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
Login or register to post comments
Comments
First pass at documenting contents of $user -- Help wanted
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.
The complete list
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
ssid - (?) Single Sign-on ID
I am taking a guess.
Secure session ID
Secure session ID
If this is any help, the data
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.
Is the user logged in?
if ($user->uid == 0) {//user is not logged in
}
But there is a function for that (user_is_logged_in)
See:
http://api.drupal.org/api/drupal/modules--user--user.module/function/use...
use global $user variable in basic page
Hi all!
How can I use the global $user variable in a basic page (with php code as the format text)?
use global $user variable in basic page
have you an answer please? how can i use global $user variable to retrieve the name, address ...
thanks
accessing user roles.
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
why array
Would it be necessary to have more than one role? Can Authenticated see things Admin can't or Anon see that Authenticated can't? Maybe for custom roles?
Is it correct to say global
Is it correct to say global $user represents 'logged-in' user? If a user is not logged in then $user represents the currently not logged-in user (with uid 0), correct? Then global $user really represents both logged-in and not logged-in users. Am I missing something?
Not Quite
I'm kinda confused about what you're asking, but I'm going to attempt to help.
"global $user" doesn't necessarily represent a logged in user. Anonymous users (those who haven't logged in) are still users, but don't necessarily have all their user fields set to a value.
It is true that the uid for an anonymous user is 0, but the role, session, and other user fields also have values too.
My point is the documentation
My point is the documentation is not entirely correct. Should 'logged-in' be removed in the phrase 'An object representing the currently logged-in user.'?
$user represents the current user, whether logged in or not. As you mention, $user includes session and role data about anonymous users that are not logged in.
Custom User Fields
I have created custom user fields (first name, last name) for my application. How would I get the values for those without having to write a database query?
Try
Try http://api.drupal.org/api/drupal/modules%21user%21user.module/function/u...