Code tip: accesing Joomla user data

  • Font size: Larger Smaller
  • Hits: 16006
  • Print

Sometimes our calculators are only available for logged in users. It's a good idea to show at the calculator form or results some data that we have already available, instead of asking again user name, email, or some other redundant information at our calculator form.

For this purpouse we are going to use at our code section the JFactory Joomla class to access current user object. At our code section, we need to add:

$user = JFactory::getUser();

With this simple line we've recovered all the information we have from the current user. This object contains all information regarding the user, so we can use it to get all these values:

$userid= $user->id; //The unique, numerical user id
$username= $user->name; // The name of the user
$userusername= $user->username; // The login/screen name of the user
$useremail= $user->email; // The email address of the user.
$userpassword= $user->password; // The user's password (encrypted)
$usergroups= $user->groups; // List of Joomla groups this user belongs to (is an array)
$userblock= $user->block; // Returns '1' if this user is blocked
$userregisterDate= $user->registerDate; // Date when the user was first registered.
$userlastvisitDate= $user->lastvisitDate; // Date the user last visited the site.
$userguest= $user->guest; // Returns '1' if the user is not logged in.
$userlastResetTime= $user->lastResetTime; // Last time the password was reset.
$userresetCount= $user->resetCount; // Counts the number of password resets.

Now we are able to use this information, to show it at the input form (using inline results), and/or at the output form. For ex, with this configuration at the exit layout:

This is your user information
Your user id is : ##userid##
Your user name is : ##username##
You email is : ##useremail##
You've reset your password ##userresetCount## times

You'll show user information at the result section when calculate button is pressed. This can be very useful to automatically fill your quote pdfs, and even send automatically an email to the logged in user email, placing $useremail at the email configuration as the destination parameter.

Last modified on

Our clients' feedback