2. A basic overview of the variables
3. Variable usage
$userdata variables are used to display information about a logged in user. They are obtained from the columns from the phpbb_users and phpbb_sessions table.
For example, to create a simple display of the logged in users username, you could use this code within your PHP script (note: the PHP script must be integrated into phpBB).
For registered users, their actual username would be displayed, while guests would see output of the text "Anonymous".
Below, is a list of all variables you can use, each serving a different purpose. Each of the following columns must be used in association with the $userdata prefix, so it appears in the format $userdata['column_name']
For instance, $userdata['user_id'], $userdata['user_active'], $userdata['username'] are all valid userdata variables, which will all output a value specific to the user viewing the page.
A basic overview of the variables
The user id.
Has a users account been activated.
The username of the user.
md5 hash of the user password.
The unix timestamp of the last session update.
The last page the user has visited.
Unix timestamp of a users last visit.
Unix timestamp of the date and time a user registered.
The level of a user (Guest, Registered, Moderator, Administrator)
Number of posts a user has made.
The timezone of the user
The ID of the style a user is using.
The language a user is using.
The date format a user has chosen.
Does a user have new messages.
Does a user have unread messages.
ID of last private message.
Number of failed login attempts.
Unix timestamp of last login attempt.
Can other users view this users email address.
Attach signature to posts.
Does the user wish to enable HTML for their messages.
Does the user wish to enable bbCode for their messages.
Does the user wish to enable smilies for their messages.
Is the user permitted to display an avatar.
Does a user have PM priveledges.
Is the user hidden.
Does the user wish to receive notifications.
Does the user wish to be notified of new PM's.
Does the user wish to receive a popup when they receive new PM's.
The rank of the user.
The users avatar.
What avatar type is used.
Users email address.
Users ICQ number.
bbCode uid for the signature.
AOL instant messenger address.
Yahoo messenger address.
MSN Messenger / Windows Live Messenger address.
The activation key for the user.
The md5 session id.
Hex of the users IP address.
Is the user logged in.
Is the user admin for the current session.
Some of the more common $userdata variables you may need to use. All examples rely on the fact that the page has been integrated into phpBB already.
You very often, when writing MOD's, need to check a user's ID number. This is often a very simple check you might want to make, in an SQL query for instance. Below is a query which would obtain all data from a table, where the user_id matches that of the logged in user. The $userdata['user_id'] variable does not need to have intval() applied to it as it has already been sanitized.
$sql = 'SELECT * FROM ' . MY_MOD_TABLE . ' WHERE user_id = ' . $userdata['user_id'];
Suppose you wanted to make a very basic script which welcomes a user to a page, by saying "Welcome, Username!". You could use this script:
echo 'Welcome, ' . $userdata['username'] . '!';
If you want to make a page visible only to a certain group of users, you can user the user_level variable. The major groups of users are guests, registered users, moderators and administrators. In phpBB, these are represented by the constants ANONYMOUS, USER, MOD and ADMIN respectively. Suppose you want to make a certain page visible only to administrators (ADMIN) you would use this code:
if( $userdata['user_level'] != ADMIN )
message_die(GENERAL_ERROR, 'You must be an administrator to view this page');
Should you want to display the number of posts a user has, you can use this variable. An interesting way that it can be implemented, is as a bbCode. By adding the code below just before the [CODE] and [/CODE] for posting code
section in includes/bbcode.php, it will allow users to type [youposts] as a bbCode into your forum, and whoever reads the post, will see their number of posts be displayed. When would you use this you might ask? Well, maybe never, but it sure is fun if you put something like "[youposts] reasons to post on this forum", and some users will see "4281 reasons...", some will see "10 reasons...", some will see "0 reasons...". It depends on their post count!
$text = str_replace("[youposts]", $userdata['user_posts'], $text);
Like the user_level variable, this code is a very quick and effective way to determine if a user is logged in.
if( !$userdata['session_logged_in'] )
message_die(GENERAL_ERROR, 'You must be registered and logged in to view this page');