How to get the logged in users id and username?

How do I get the ID and username of the currently logged in user?

Category: Development 3 years ago
Omni
Asked 3 years ago
Omni

To get the logged in users id: JFactory::getUser()->id;

To get the logged in users username: JFactory::getUser()->username;

Alternatively, you can set the user object into a variable for repeated use.

For example-

$user = JFactory::getUser();

Then access it anytime like this-

echo $user->name;

echo $user->email;

echo $user->id;

etc.

Explore the user object with a variable dump.

var_dump($user);

To see all the values contained in the user object.

If you want to use a users data that is not the currently logged in user,

simply put their id in as an arguement - 

JFactory::getUser(1037)->username;

Have Fun! 

Omni
Answered 7 months ago
Omni

Please register/login to answer this question.  Click here to login
Cron Job Starts