To determine if the current user is logged in and obtain informations such as username, email, user type, you need to get a reference to the global User Object (an instance of the class JUser of Joomla! framework) and access its properties.
- id value of ID field in users db table.
- username registered username.
- name personal name.
- usertype a string having one of the following values: Registered, Author, Editor, Publisher, Manager, Administrator, Super Administrator.
- guest it's True if current user is browsing the site as guest (id has a value of 0 (zero) and all the other properties are empty); it's False if current user is logged in.
Here is the code:
$user =& JFactory::getUser();
echo $user->id;
echo $user->username;
echo $user->name;
echo $user->email;
echo $user->usertype;
echo $user->guest;
This code works in components, modules (installable modules not Custom HTML modules you create from Module Manager in Joomla! backend) and templates.
For example with the following piece of code included in index.php of your template you can show an information to guests or logged in users only.
$user =& JFactory::getUser();
if($user->guest) {
echo 'Only guests will see this';
} else {
echo 'Only logged in users will see this';
}
| < Prev | Next > |
|---|






