views:

350

answers:

3

Hi there, I am trying to create a way to show and hide a number of different elements on my page, depending if the user is logged in or not.

For example I want to hide 'Logout' is users are not logged in, and 'login' when they are.

I'm using Coldfusion and Dreamweaver - is there any quick easy code I am able to use to wrap around the page elements I want to hide?

Thanks for any help. Georgia.

A: 

If you are using the standard CFLOGIN built into Coldfusion you can show/hide elements by checking for a logged in user:

<cfif GetAuthUser() neq "">
      Show Logout button
</cfif>

http://livedocs.adobe.com/coldfusion/8/Tags%5Fj-l%5F07.html

jarofclay
Hi. Thaks. That does work by hiding the logout link - however, when I then login, the 'logout' button still does not show? Any ideas to why not? Thanks for your help.
Lucy
Check to make sure you are actually logged in. <cfoutput>#getauthuser()#</cfoutput> If nothing shows up then you are likely not logged in.
jarofclay
A: 

If you are using Dreamweaver's Log In User server behavior, then it creates a session variable named MM_Username (Session.MM_Username) that contains the user name from the log in form. When that variable exists and is not an empty string, then the user is considered logged in. All you should need to do is to check for the existance of that vairable and it not being an empty string.

My CF is rusty, and I don't have a system with ColdFusion installed to be able to give you tested code, but it should be something along the lines of the following:

<cfif IsDefined(Session.MM_Username) And Session.MM_Username NEQ "">
 Logout link here
<cfelse>
 Log in link here
</cfif>

FYI: The Log Out User server behavior sets that session variable to an empty string rather than destroying the variable, that's why you need to check for it not being an empty string using the Dreamweaver Log in/out server behaviors.

Danilo Celic
+2  A: 

Generically, if you have a session variable called "loggedIn" and (assuming it's boolean) it's as simple as:

<cfif session.loggedIn>
  <!--- display logged in code --->
</cfif

OR

<cfif NOT session.loggedIn>
  <!--- display not logged in code --->
</cfif>

But, I mean, it really depends on how you're tracking whether a user is logged in or not.

Al Everett