Currently, I successfully added a login feature to my website. Now, on every page, I have the following code in the upper right hand corner of my page: <cfif not isDefined("SESSION.auth.USER_NAME_NAME")> <cfoutput> <p><a href="login.cfm"><strong>Login</strong></a> or <strong><a href="register.cfm">Register</a>!</strong> </p></cfoutput> <a href="why.cfm"> <p class="style5">Why register?</p> </a> <cfelse> <cfoutput><p>Welcome <strong>#SESSION.auth.USER_NAME_NAME#</strong>!<br /> | <a href="#t#my_account.cfm">My Account</a> |</p> </cfoutput> </cfif> Code (markup): It works fine. The session varialble is defined upon login on the login.cfm page. Now, what I'd like to add to this code is "Log off". Upon clicking "Log off" it will terminate the user's session, refresh the page, and the other default text "Login or register!" will appear. How can I do this?
Try this: Create a link to a page called logout.cfm like this <a href="logout.cfm">Logout</a> Code (markup): The some code for the logout page: <cfset structdelete(session,"userid",true)> <cflocation url="where ever you like"> Code (markup): I have all sorts of stuff on my logout page, things like saving their last logout and how long they were on, Its where you can do all the clean up,
So in RED above, is that just the last portion of the variable name, or does it need to be the entire variable name (for me, SESSION.auth.USER_NAME_NAME)? Also, for this: I would like it to just take them back to where they came from. So, I could put in the javascript history code, but the problem is, it sends the user back to the previous page WITHOUT refreshing the page. Therefore, session variables will still exist on the page (because it hasn't been reloaded to update the changes)! How can I get it to refresh? The meta refresh code doesn't work, because that would go on forever...
you could pass the current page they are on to the logout page as a url variable to dynamicaly fill in the cflocation tag. Like this <cfoutput> <a href="logout.cfm?page=#CGI.SCRIPT_NAME#">Logout</a> </cfoutput> Code (markup): The at the end of the logout page you would still the cflocation tag <cfoutput> <cflocation url="#URL.PAGE#"> </cfoutput> Code (markup): That way when a users clicks the logout button from any page, the page name will be passed to the logout page to tell the logout page to take them back to where they came from.
OK, awesome. I got that to work. It now updates the ONLINE an LAST_LOGIN_DATE when I click logout. However, it still holds the session variables. AKA, it still thinks the user is logged in. How can I delete the session variables? I think it's structdelte or something? But, I also heard, this may not be the best way to terminate a session. For instance: I have this on my account page, where they can click "LOGOUT": <cfoutput> <a href="logout.cfm?page=#CGI.SCRIPT_NAME#">Logout</a> </cfoutput> Code (markup): Then I have this on the logout page: <cfif isDefined("SESSION.auth.USER_NAME_NAME")> [COLOR="Red"] <cfquery datasource="mydatabase" name="last_login"> UPDATE TBLUSER SET LAST_LOGIN_DATE = #CreateODBCDateTime(NOW())#, ONLINE = 0 WHERE USER_NAME_NAME = '#SESSION.auth.USER_NAME_NAME#' AND ONLINE = 1[/COLOR] </cfquery> <cfset structdelete(session,"USER_ID",true)> <cfset structdelete(session,"USER_NAME_NAME",true)> <cfset structdelete(session,"USER_PASS_PASS",true)> <cfset structdelete(session,"USER_FAV_RECIPE",true)> <cfset structdelete(session,"USER_FIRST_NAME",true)> <cfset structdelete(session,"USER_LAST_NAME",true)> <cfset structdelete(session,"USER_GENDER",true)> <cfset structdelete(session,"CLIENT_FILE_NAME",true)> </cfif> <cfoutput> <cflocation url="#URL.PAGE#"> </cfoutput> Code (markup): The portion in RED works fine. The rest doesn't. Because it keeps the user on the account page with all the information.... instead, since the user logged out, it should prompt them to log in again before displaying the information.
Try changing this <cfif isDefined("SESSION.auth.USER_NAME_NAME")> Code (markup): To this <cfif structKeyExists(session, "user_ID") AND session.user_ID NEQ ""> Code (markup):