How to Retrieve the Non-Cached User Information from Joomla?

First we apologize that this post is very technical, but this is a recurring issue and we thought we’d share the solution with others that may be experiencing this problem.

The easiest way to retrieve the information of the current logged in user in Joomla is the following:

$user =& JFactory::getUser();

But there is a problem in the above code. Let’s say, for example, that the user information was updated by another thread while the user is logged in to your website (this other thread might be automated, such as a cron job, or might be someone manually updating the user information in the backend). The problem is that when an external update happens on the user, the $user object remains the same. So, if the phone number is changed by that external thread, this change is not reflected in the user object. This is because Joomla caches (in the session) all the information about the currently logged in user. Fortunately, there is an easy way to solve this problem, and it is by using the following code instead:


$user =& JFactory::getUser();
$user_id = $user->id;
$user =& JFactory::getUser($user_id);

What we do is that we first get the user (using the JFactory::getUser() with no parameters), then we get the user id of that user (the user id never changes), and then we get the user based on the user id. This way we will fool Joomla into thinking that we want some information on a specific user in the jos_users table, regardless if that user is logged in or not, which will make Joomla lookup the user in the table directly (based on his user id), and not use the cached version of the user.

No comments yet.

Leave a comment