News Feed E-mail

Get How-To's right in your inbox. Subscribe to the HowToJoomla! news feed e-mail. Just enter your email address here:

Delivered by FeedBurner

Login






Lost Password?
No account yet? Register

Affiliates

Azrul's JomComment
JoomlaShack
iJoomla
The Joomla! Store
Member of W3C Sites dot com
Making module positions viewable only by unregistered visitors PDF Print E-mail
Written by Cory   
Tuesday, 12 September 2006

Here is a trick for making your module positions viewable only by unregistered visitors.

There are some powerful programming objects in the Joomla! API that are available to template designers. One such object is the user object, which is accessible with the php variable $my.

If you want to determine whether or not a visitor is a registered user, you simply need to add the following code:

 
<?php
 
  if ( $my->id ) {
    // Code to generate if visitor is a registered user
  } else {
    // Code to generate if visitor is not a registered user
  }
 
?>

This code basically just says "If this visitor has a user id, then generate the first set of code. Otherwise, generate the second set of code."

We can now take this idea one step further. Let's use this trick we've just learned to tell our template not to display a module position to registered users.

 
<?php
  if ( !$my->id ) {
    mosLoadModules( 'guests_only_position', -2 );
  }
?>

Notice the "!" before $my in this example. You interepret this by adding the word not. So this code basically says, "If this visitor does not have a user id, then load the modules that are assigned to the module position 'guests_only_position'." (Please note that "guests_only_position" is not a standard module position in Joomla!. I only used it for the sake of this example.)

This trick is useful if you have items that you do not want to show your registered users. For example, you might have a banner inviting visitors to join your site. Your registered users do not need to see this banner, so you can use this trick to hide it from them.

There are a number of possible uses for this trick in your Joomla! template. I will leave it up to you to come up with more creative ways to use it.

Trackback(0)
Comments (37)add comment
Dan: ...
Great little hack, thanks for sharing. Worked like a charm for me smilies/smiley.gif
1

report abuse
vote down
vote up
December 27, 2006
Votes: +0
Hi Dan... I'm glad you found it useful! smilies/smiley.gif
2

report abuse
vote down
vote up
December 27, 2006
Votes: +0
Manu: ...
Very simple & very usefull !
Thanks smilies/grin.gif
3

report abuse
vote down
vote up
January 17, 2007
Votes: +0
rick dias: ...
very nice just what i needed, thx smilies/cheesy.gif
4

report abuse
vote down
vote up
January 30, 2007
Votes: +0
You're welcome! smilies/smiley.gif
5

report abuse
vote down
vote up
January 30, 2007
Votes: +0
Awesome! I have just started using Joomla and have been looking for this for a long time!! Thanks Man!
6

report abuse
vote down
vote up
February 22, 2007
Votes: +0
How about putting this in the head then?



I have this in a test template and can be used for just about anything. The main idea is to have different css for public and registered users. The php files just add css to the head. Or you can simply echo a link to the appropriate stylesheet. Including phph files however also allows me to perform different php for public and registered.

Now it's just been used for emphasizing certain divs for the public, while visually moving them to the background for registered users.
7

report abuse
vote down
vote up
March 03, 2007
Votes: +0
O no code allowed? Okay pseudo code placed in the template's head

if logged in
include public.php
else
include registered.php
8

report abuse
vote down
vote up
March 03, 2007
Votes: +0
Hi Dylan... You should be able to do that. The "if ($my->id )" code will work anywhere in your template index.php file.
9

report abuse
vote down
vote up
March 03, 2007
Votes: +0
Yes, I got it working. Thanks for sharing.

I was initially trying to get working within a module itself, but that didn't work. Now I got it in the template file and just needs combining with my 'dynamic css' scripts. Calculates container divs sizes and positions based on browser width (js/cookies). You can probably also hide/show things completely for public/registered with css like "display:none" ... but for now have no use yet for the latter.

But I'm sure I'll find plenty more uses for different php (and css) for public and registered.
10

report abuse
vote down
vote up
March 03, 2007
Votes: +0
You can probably also hide/show things completely for public/registered with css like "display:none"


The problem with that is, the content is still viewable in the HTML code. If you truly want to hide it, the best way is to use PHP so that the content is never passed via HTML to the browser.
11

report abuse
vote down
vote up
March 03, 2007
Votes: +0
True. Was just thinking out loud. You're right.

In some cases I just want different css, let's say a large interstatial for public and a 'background' banner for registered. Or in other cases, let's say a splash screen or take-the-tour- div on first visit, you might be better off with cookies. In some cases you might be better off with doing the if logged in loop in the body, as to show or hide something alltogether ... Or in some cases you might combine it all.

As to what originally raised this issue for me was different css for public and registered. Now the next ultimate cool thing would be if I could assign different css (or php or java or anything else for that matter) based on user-type. Different css for each different userlevel, including custom levels (ie using JACLPlus). That ... well that would be ultimaltely cool smilies/cool.gif
12

report abuse
vote down
vote up
March 03, 2007
Votes: +0
Btw Cory, compliments all round to you. And thanks!! Had a quick glance here and there and found some very useful little things. You are a true master of getting things done the simple way. Furthermore what an excellent choice of selection.

"Long live Sir Cory" !!!
13

report abuse
vote down
vote up
March 03, 2007
Votes: +0
Thanks Dylan. I'm blushing. smilies/smiley.gif

With what you are talking about doing, you might want to look into learning how to code a plugin (aka mambot). There is a way with a plugin to put markup inside the "head" tags of your HTML, so you could reference CSS files and/or JS files based on the level of the person visiting the site. That sort of thing is probably too advanced to add in the template.
14

report abuse
vote down
vote up
March 03, 2007
Votes: +0
Ah, yes indeed Cory. A mambot. I hadn't thought bout that. :eureka: and :thumbsup:

For now I can do with what I have. Primary desire was to absolutely position all divs and absolutely define all containers. And for some have a fixed width or height, ie a header, pics in a photo gallery. So I started with JS for the browser width and cookies to get it to php. Then include a settings file where I can 'configure' min width, max width, fixed sizes &such. Then calculate all positions and sizes simply by going if module exist move and or narrow the next div so many pixels down or to the right. Took some time since I'm relatively new ... well at least compared to you smilies/wink.gif ... but got it. But I got the hang of it.

Next I just wanted to whisper instead of shout a specific (html) module once the user logs in. No need to shout to logged in users. I assume they know what the site's about. That's all. And since I already had the dynamic css for positioning/sizing I thought I'd just plugin that little bit extra. Whisper vs. shout. After all, only a very little bit of extra css.

Sorry to go on so long but hey you did say ...
I will leave it up to you to come up with more creative ways to use it


Basically I just see the following loop which can be used to serve up content as well as presentation, shout vs. whisper

if not logged in

use javascript/cookies to determine everything you can/need
add public rules
add context-relative rules
// ready to serve up content, presentation, ads ...

elseif logged in

can still use js/cookie // ie browser width
add registered rules

// loop for several components, ie cb, e-commerce stuff, tracking stats, acl
foreach extension ( if (userinfo not empty (get userinfo) )

add context-relative rules
// ready to serve up content, presentation, ads ...

endif

Serve it up. Enjoy your meal. So you can do anything with this if else loop, just set off a set of rules and logic to determine what content to show and lest not forget -how- to show it. Can be used bot for helping the user. ie On an automobile site, to determine what type of car the user is into (convertible|suv|sports) you might be able to use his profile, his history, something set from doing a poll (i drive a...) or just about anything else to serve up content weighing it accordingly to the users info. To enhance user-friendliness: Hey you might like this article, forum post, you might even like these members who also drive a fabulous audi convertible just-! like-! you-! ... as to enhance ads-friendliness/banner targeting: hey, you might like this banner

And having to do some dynamic css anyway, you might as well throw in some random rules as well, i.e. to rotate font-color used for text-ads? Just a thought. Hey, why settle for just the related items module?

In short, what a nifty little trick .. that if logged in loop. And personally, I just have to watch out that I don't totally go hogwild.
15

report abuse
vote down
vote up
March 04, 2007
Votes: +0
Vince Cardillo: ... http://www.vincecardillo.com
THANK YOU! I just read this article after poking around forever in the Joomla documentation. I love Joomla but, seriously, their documentation sucks sometimes. I've been trying to figure out this little trick forever!

Thanks so much.
16

report abuse
vote down
vote up
April 11, 2007
Votes: +0
Hi Vince,

You're welcome. I know how frustrating it can be sometimes to find information in the documentation. That's why I started this site to provide as much help as I can. I'm glad you found it useful.

Cory
17

report abuse
vote down
vote up
April 11, 2007
Votes: +0
paul: ...
What if i want to hide a specific module from registered users (not the entire module position)... can it be done ?
18

report abuse
vote down
vote up
April 28, 2007
Votes: +0
Paul,

That functionality is currently not possible in Joomla. I wish it were possible. The only way to come close is to get creative with where you put your module positions, and which module positions you hide from registered users.

Cory
19

report abuse
vote down
vote up
April 28, 2007
Votes: +0
max: ...
Is it possible to change the whole look and feel of your web page depending on which page/section of a site you are on
20

report abuse
vote down
vote up
May 13, 2007
Votes: +0
Sort of confused by this - does anyone want to do this for me - willing to pay. smilies/grin.gif
21

report abuse
vote down
vote up
June 11, 2007
Votes: +0
Cory,
Thanks! This works well!!!

Max, try if a switch on the Itemid

if Itemid = x do
what you want to do
else
what else you want to do
endif

22

report abuse
vote down
vote up
July 19, 2007
Votes: +0
max,
yes - to change site based upon section view :
http://www.joomlatribune.com/joomla/sections-with-different-templates.html

uses multiple templates per site and assigns per menu item.

in action on my site.... smilies/smiley.gif
23

report abuse
vote down
vote up
August 31, 2007
Votes: +0
Cory,

That's a very useful little hack; you might want to add that $my->id actually holds a value that tells you what kind of user is logged in -- that allows you to further slice and dice your modules or content on the fly. I've got a client site where modules need to appear and/or disappear depending on whether you're an anonymous visitor, a registered user, or an "insider" (e.g. an intranet kinda deal).

The two levels of registration maps to "registered" and "special" in the user setup and can be sniffed out via the value of $my->id
24

report abuse
vote down
vote up
September 20, 2007
Votes: +0
Brian: ...
Where do I add this code? I created a "Register now" module and want to hide it from registered users. So, where do I enter your code? Thanks!
25

report abuse
vote down
vote up
October 01, 2007
Votes: +0
Hi Brian,

You add the code anywhere in your template that you want your module to display.
26

report abuse
vote down
vote up
October 01, 2007
Votes: +0
Brian: ...
Cory, sorry to be dense, I hate answer questions over and over, and I'm sorry to ask you to, but I have to ask again because I'm not sure I understand.

I created a register module, basically an additional module saying "Register now to access this and this..."I have the module placed on the right hand side. But, registered users see this too and they end up clicking it and getting the "not authorized" thing.

I don't want registered users to see this particular module. I don't know where in the template to add this code? So if I'm using rhukII default template, would I just edit the html through joomla backend and stick this wherever? I tried that and it didn't work. I'm not sure if I incorrectly named the module or what, but I couldn't get it to work.

Thanks Cory!
27

report abuse
vote down
vote up
October 01, 2007
Votes: +0
Hi Brian,

You would need to use a different module position than "right", because right is already being used by your template. Basically, in your template's "index.php" file, you would add the "if ( !$my->id)" code from this tutorial, and use a module position that is not already in use by the template. Then, you would publish your "Register" module in that position.

You can literally put it anywhere you want in your template. If you want it on the right hand side, you could put it under or above the "right" module position.

I hope that makes sense.
28

report abuse
vote down
vote up
October 01, 2007
Votes: +0
HOW TO LOAD A MODULE DEPENDING OF THE LANGUAGE



29

report abuse
vote down
vote up
October 20, 2007
Votes: +0
HOW TO LOAD A MODULE DEPENDING OF THE LANGUAGE





30

report abuse
vote down
vote up
October 20, 2007
Votes: +0
Hi Locoman,

You would have get the language parameter by using this:

$mylang = mosGetParam($_REQUEST,'lang', '');

Then, wherever you want to load a module, you would just check $mylang using "if ( $mylang == 'name of language here')"

Good luck!
31

report abuse
vote down
vote up
October 21, 2007
Votes: +0
Brian: ...
I got it working, pretty simple once you figure out the template. Thanks! One of the best hacks for Joomla!
32

report abuse
vote down
vote up
November 01, 2007
Votes: +0
Paul: ...
Easy and usable. Thanks!
33

report abuse
vote down
vote up
November 23, 2007
Votes: +0
John Scott: ... http://www.SavvyMart.com
Hi Cory,
Quick question - does the code test whether they are a registered user or whether they are logged in?

I am wanting to show/hide instructions (within content), ie "You must be logged in to download this file" depending upon whether the user is logged in. I found a mambot moshidefrom, but it required MAL component to work. I will see if I can adapt your "argument".
34

report abuse
vote down
vote up
January 02, 2008
Votes: +0
Hi John...The code tests only whether or not the user is logged in.
35

report abuse
vote down
vote up
January 02, 2008
Votes: +0
Joost: ...
Hi Cory,
Thanks for sharing this simple but very useful tip.
On a related note: how would you test for a user visiting your site for the very first time so you could serve them a kind of splash page/ intro module?

Thanks!
36

report abuse
vote down
vote up
June 25, 2008
Votes: +0
Hi Joost, the easiest way to do that (as far as I know) would be to write a cookie to that person's machine, which would tell you that the person had visited your site before.
37

report abuse
vote down
vote up
June 25, 2008
Votes: +0

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy
 
Tag it:
Delicious
Digg
Technorati
< Prev
Phil-A-Form

About HowToJoomla

HowToJoomla was started in September 2006 by me, Cory Webb, a long-time Mambo/Joomla user with a desire to give back to the community that has given so much to him.

HowToJoomla is not the best, most comprehensive Joomla tips site out there. That's not the goal. The goal is to share some of the knowledge and experience that I have gained through years of using the world's best content managment system. I hope that you will find this site useful and visit often. As I come across new tips and ideas, I will post them here for all to see.

I hope you enjoy using HowToJoomla and find it very useful.

Please take some time to read the Disclaimer.

Thanks for visiting!

About Me

I am a 28-year-old husband and father of a beautiful baby girl named Lucy. I have a degree in Electrical Engineering from the University of Texas at Austin, an MBA from Baylor University, and over 4 years experience working with the Mambo/Joomla CMS.

On February 1, 2008, I started a web design, development, and consulting company specializing in designing and developing custom Joomla-powered websites. The new company is aptly named Cory Webb Media, LLC. The double meaning is purely unintentional, but with a last name like "Webb" it is difficult to escape the obvious puns. If you are looking for professional assistance with your website, please feel free to contact me to discuss your project.

I also run HowToJoomla, CoryWebb.com (my personal website/blog), and JoomlaForm.com (a site dedicated to promoting good design in the Joomla! community). In my spare time (if there is such a thing), I volunteer with the youth ministry at my church.

Copyright © 2006 - Cory Webb Media, LLC - All rights reserved.
Powered by Joomla!