First we need to make available information (aka accessing the user object) about the user, such as their name, or in this case, their user level.
To do this, we insert the following in the head of our template:
$user =& JFactory::getUser();
We can now echo all sorts of information about the user in the template, for example:
<p>Hi <?php echo $user->name ?>, you haven't logged in since
<?php echo $user->lastvisitDate ?>.</p>
Remember, this has to be in the template index.php, you can’t echo this in an article or module as it needs to be processed at the template level.
Showing Different Content to Different Users
So now we use some CSS…
In our head we put:
<?php
if( !$user->guest ): ?>
<style type="text/css">
.logguest {display:none;}.logmember {}
</style>
<style type="text/css">
.logguest {}.logmember {display:none;}
</style>
<?php endif; ?>
Now, anything I want only guests to see, I surround with a class of logguest, anything I want only a logged in user to see I put a class of logmember. More importantly, because its now CSS, I can put it in a module or article!
For example:
<p class="logguest">
Dude, register already!
</p>
<p class="logmember">
Hi <?php echo $user->name ?>, you haven't logged in since
<?php echo $user->lastvisitDate ?>.
</p>
One thing to bear in mind, the content is still there, but its just not being shown via CSS, so Google will still see it (for example). Its a crude, but simple technique.
Other Content Hiding Options
If you need more control over what content is shown to what users, check out David Towers excellent review of JoomiHide, Ninja Access – JACL and RokAccess
Other User Object Variables
The full list can be found on the Joomla wiki:
- id – The unique, numerical user id. Use this when referencing the user record in other database tables.
- name – The name of the user. (e.g. Vint Cerf)
- username – The login/screen name of the user. (e.g. shmuffin1979)
- email – The email address of the user. (e.g. crashoverride@hackers.com)
- password – The encrypted version of the user’s password
- password_clear – Set to the user’s password only when it is being changed. Otherwise, remains blank.
- usertype – The role of the user within Joomla!. (Super Administrator, Editor, etc…)
- gid – Set to the user’s group id, which corresponds to the usertype.
- block – Set to ‘1’ when the user is set to ‘blocked’ in Joomla!.
- registerDate – Set to the date when the user was first registered.
- lastvisitDate – Set to the date the user last visited the site.
- guest – If the user is not logged in, this variable will be set to ‘1’. The other variables will be unset or default values.
This article has an SEO score of 100% and content, joomla, users, and show are emphasized within the content and are considered Primary Keywords.
Find out more about Scribe for Joomla