Show a module when a particular user is logged in

(4 votes, average 4.50 out of 5)

Let’s say you want everyone to know when a particular person is logged in to the system (e.g. for technical support). This recipe allows you to display a module for as long as this person is logged in to the system!

Joomla keeps track of all logged-in users. If you log out explicitly, then your entry is deleted from the session table immediately. If you allow your login to just expire without logging out, then this method thinks you are logged in until your session expires. Login session length is controlled in the Joomla backend.

Don’t set it for really long times, or else this method will think that your user is still logged in until far after they have gone!

// change some_guy23 to the login name of the person you are tracking
$search_user_name = 'some_guy23';
$username = $db->getEscaped($search_user_name);
$query = "SELECT username "
. " FROM #__session WHERE username = '$username' "
. " AND guest = 0 "
. " AND client_id = 0 ;" ;
$db->setQuery( $query );
$found = $db->loadAssoc();
if (is_array($found)) return 101; /* found him. customise to your own module number. */
return 102; /* didn't find him. Customise to your own module number. */