Cmxmods.net
Would you like to see a new MOD created?

Support Forum · Home phpBB 2.0.x Support $userdata variables

Contents
1. Introduction
2. A basic overview of the variables
3. Variable usage


Introduction

$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).

echo $userdata['username'];


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



$userdata['user_id']
The user id.

$userdata['user_active']
Has a users account been activated.

$userdata['username']
The username of the user.

$userdata['user_password']
md5 hash of the user password.

$userdata['user_session_time']
The unix timestamp of the last session update.

$userdata['user_session_page']
The last page the user has visited.

$userdata['user_lastvisit']
Unix timestamp of a users last visit.

$userdata['user_regdate']
Unix timestamp of the date and time a user registered.

$userdata['user_level']
The level of a user (Guest, Registered, Moderator, Administrator)

$userdata['user_posts']
Number of posts a user has made.

$userdata['user_timezone']
The timezone of the user

$userdata['user_style']
The ID of the style a user is using.

$userdata['user_lang']
The language a user is using.

$userdata['user_dateformat']
The date format a user has chosen.

$userdata['user_new_privmsg']
Does a user have new messages.

$userdata['user_unread_privmsg']
Does a user have unread messages.

$userdata['user_last_privmsg']
ID of last private message.

$userdata['user_login_tries']
Number of failed login attempts.

$userdata['user_last_login_try']
Unix timestamp of last login attempt.

$userdata['user_emailtime']


$userdata['user_viewemail']
Can other users view this users email address.

$userdata['user_attachsig']
Attach signature to posts.

$userdata['user_allowhtml']
Does the user wish to enable HTML for their messages.

$userdata['user_allowbbcode']
Does the user wish to enable bbCode for their messages.

$userdata['user_allowsmile']
Does the user wish to enable smilies for their messages.

$userdata['user_allowavatar']
Is the user permitted to display an avatar.

$userdata['user_allow_pm']
Does a user have PM priveledges.

$userdata['user_allow_viewonline']
Is the user hidden.

$userdata['user_notify']
Does the user wish to receive notifications.

$userdata['user_notify_pm']
Does the user wish to be notified of new PM's.

$userdata['user_popup_pm']
Does the user wish to receive a popup when they receive new PM's.

$userdata['user_rank']
The rank of the user.

$userdata['user_avatar']
The users avatar.

$userdata['user_avatar_type']
What avatar type is used.

$userdata['user_email']
Users email address.

$userdata['user_icq']
Users ICQ number.

$userdata['user_website']
Users website.

$userdata['user_from']
Users location.

$userdata['user_sig']
Users signature.

$userdata['user_sig_bbcode_uid']
bbCode uid for the signature.

$userdata['user_aim']
AOL instant messenger address.

$userdata['user_yim']
Yahoo messenger address.

$userdata['user_msnm']
MSN Messenger / Windows Live Messenger address.

$userdata['user_occ']
Users occupation.

$userdata['user_interests']
Users interests.

$userdata['user_actkey']
The activation key for the user.

$userdata['user_newpasswd']


$userdata['session_id']
The md5 session id.

$userdata['session_ip']
Hex of the users IP address.

$userdata['session_user_id']


$userdata['session_logged_in']
Is the user logged in.

$userdata['session_start']


$userdata['session_admin']
Is the user admin for the current session.

$userdata['session_key']



Variable usage

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.

$userdata['user_id']

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'];



$userdata['username']

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'] . '!';


$userdata['user_level']

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');
}



$userdata['user_posts']

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);


$userdata['session_logged_in']

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');
}