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
First pass at documenting contents of $user -- Help wanted
PermalinkThe 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
PermalinkFrom 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
Permalinkssid - (?) Single Sign-on ID
I am taking a guess.
Secure session ID
PermalinkSecure session ID
If this is any help, the data
PermalinkIf 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?
Permalinkif ($user->uid == 0) {//user is not logged in
}
But there is a function for that (user_is_logged_in)
PermalinkSee:
http://api.drupal.org/api/drupal/modules--user--user.module/function/use...
accessing user roles.
Permalinkif 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
user picture with user name
Permalinki wana use user picture with user name in drupal 7,if any one have any solution plz send me....thanks
uid
PermalinkI would like to ask how to set uid by ourselves when we great new account?
The user ID is owned by the
PermalinkThe 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
PermalinkActually, 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);
How is an user uid generated?
PermalinkThere 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')
How to tell if a user just logged in?
PermalinkI 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.