Joomla – How to check if a user is logged in

Joomla – How to check if a user is logged in

I recently came across a requirement within a Joomla site for an in-page user verifier to check if a user is logged into the website or not.

This type of facility allows the website to grant registered joomla users the ability to easily download a pdf documents through the docman extension whereas joomla guest users will be shown login or registration details.

The first part of the solution is to download and install the Jumi component for Joomla, the installation will also automatically install the Jumi module.

I won’t go into the installation process here as it is documented online, but once installed, in the Jumi component, add a new code listing from the main Jumi application manager page.

Enter this code into the Custom Script field:

if(JFactory::getUser()->guest) {
 //do user logged out stuff
 echo "you are NOT logged in";
 }
 else {
 //do user logged in stuff
 echo "you are logged in";
 }

Then with some knowledge of PHP add your own display code, see the example above where I have added the ‘echo’ tags.
Once you have added you ow code, save this Jumi details page and then take a note of the ID of the new code listing on the main Jumi applications manager page.

Now go to the Module Manager, and click the “+” button upper right to open a new module, select Jumi as the module.
Once in the Jumi module page, enter a name for the module and in the right hand parameters Basic Options, add the Jumi ID number from the components screen preceded with *, there:
*9
On the same page in the Position field, write your own position, make sure it is not already used by the template, for instance:
“jumi_k2_in_item_module”
Save & Close.

Now make your way to the K2 item you want the logged in / out code to be inserted into.
Within the K2 item, coding level, add this code:
{loadposition jumi_k2_in_item_module}
Make sure you include the curly brackets.
Save the K2 item and preview the page.

You should now see text either for logged in users or logged out users depending on whether you are logged in to not. Try changing your login status to see what text is displayed within the K2 item.

I have used this exact same technique to place a pdf download button within the K2 item if the user is logged in, if the user is not logged in then they are presented with a request to login or register.

A very simple technique that should help you to designate content for different user levels.

  1. Login to your Joomla installation
  2. Site menu
  3. Global Configuration
  4. Server tab
  5. Server Settings
  6. Now set the Error Reporting to ‘None’.

This removes any php errors from now being displayed on your Joomla sites!

A very simple solution which I just could not find anywhere.