Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2008
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question If Statements for showing things to logged in users

    I'm somewhat of a coldfusion novice trying to build quite a complicated system (bad combination!) but I need to do it by next Thursday so this is why I'm asking. I've got books and have made my login system, and i have an include called rightside.cfm. I want it to show a login form if the user isnt logged in, and a logout button etc if they ARE logged in. I know this is probably SO simple but I can't figure out what my ifstatement should say.

    I have application.cfm,
    login.cfm,
    login2.cfm

    Application.cfm:
    Code:
    <cfapplication name="StickLogin"
    sessionmanagement="yes"
    setclientcookies="yes"
    sessiontimeout="#createtimespan(1, 0, 0, 0)#"
    applicationtimeout="#createtimespan(2, 0, 0, 0)#"
    clientmanagement="yes"
    clientstorage="registry">
    
    
    <!--- server constants that can be accessed by any client---->
    <cflock timeout="30"
    throwontimeout="yes"
    name="server"
    type="exclusive">
    <cfif not #isdefined("server.season")#>
    
    <cfset server.season = "Spring Time">
    </cfif>
    </cflock>
    
    <!---set application constants--->
    <cflock timeout="30"
    throwontimeout="yes"
    name="#application.applicationname#"
    type="exclusive">
    <cfif not #isdefined("application.started")#>
    <cfset application.title = "My Login App">
    <cfset application.db = "062105cs06sr">
    <cfset application.email = "suzierthanyou@gmail.com">
    <cfset application.started = "true">
    </cfif>
    </cflock>
    
    <!----Test to see if user has logged in---->
    
    <cflock timeout="30"
    throwontimeout="yes"
    name="#session.sessionID#"
    type="readonly">
    <cfif not isdefined("session.started")>
    <cfset login = "">
    </cfif>
    </cflock>
    
    <cfif isdefined("login")>
    <!---tests to see if a user is logging in ---->
    <cfset path=getdirectoryfrompath(#cgi.cf_template_path#)>
    <cfif (cgi.cf_template_path is not "#path#login.cfm")
    and (cgi.cf_template_path is not "#path#login2.cfm")>
    <cfinclude template="login.cfm">
    
    <!---aborts processing of any template except login.cfm and login2.cfm until user has logged in - im not sure I really want that--->
    <cfabort>
    </cfif>
    </cfif>
    Login.cfm:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <cflock timeout="30"
    throwontimeout="yes"
    name="#application.applicationname#"
    type="readonly">
    <cfset title = Application.Title>
    </cflock>
    
    <cfoutput>
    <html>
    <head>
    <title>#title#</title>
    </head>
    <body>
    <cfinclude template="header.cfm"></cfinclude>
    
    <form action="login2.cfm" method="post">
    <!---if client.username exists (set in login2.cfm) welcomes them back--->
    <cfif isdefined("Client.username")>
    Welcome to #title#, #client.username#! Please login to start a new session!
    </cfif>
    
    <!---Prompts user to enter their username and password--->
    Please enter your username: <br>
    <input type="text" name="username" size="20"><p>
    Please enter your password: <br>
    <input type="password" name="password" size="20">
    <p>
    <input type="submit" name="submit" value="login">
    </form>
    
    </div>
    
    
    
    <cfinclude template="rightside.cfm"></cfinclude>
    
    </div>
    </div>
    </body>
    </html>
    </cfoutput>
    Login2.cfm
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <cflock timeout="30"
    throwontimeout="yes"
    name="#application.applicationname#"
    type="readonly">
    <cfset title = Application.Title>
    <cfset db = Application.db>
    </cflock>
    
    <!---Securitycheck query verifies that form.username and form.password are valid--->
    
    <cfquery name="securitycheck" datasource="#db#">
    select users.userid,
           users.password,
           users.username
    FROM users
    WHERE users.username = '#form.username#' AND
          users.password = '#form.password#'
    </cfquery>
    
    <!----If form.username and form.password are not valid then prompt the user to log in again----->
    <cfif securitycheck.recordcount is 0>
    <cfoutput>
    <html>
    <head>
    <title>#title#</title>
    </head>
    <body>
    <cfinclude template="header.cfm"></cfinclude>
    <cfinclude template="login.cfm">
    Your username and password are not found in our database, please try again!
    </div>
    <cfinclude template="rightside.cfm"></cfinclude>
    </div>
    </div>
    </body>
    </html>
    </cfoutput>
    <cfabort>
    <!----if the username and password are verfied then the session is started, client.username is set and the user is redirected to the home.cfm template--->
    
    <cfelse>
    
    <cflock timeout="30"
    throwontimeout="yes"
    name="#session.sessionid#"
    type="exclusive">
    <cfset session.started = true>
    </cflock>
    
    <cfset client.username = "#securitycheck.username#">
    <cflocation URL="home.cfm" addtoken="no">
    </cfif>

    Thank you SO MUCH in advance for any light you can shed on this. I am learning, it's starting to make more sense, but I'm in a bit of a panic as I have to have it finished by next Thursday (9 days argh!)
    Would it be something like:
    Code:
     <cfif client.username = "#securitycheck.username#">
    or would it be to do with session? There are so many different names for things!

  • #2
    Regular Coder
    Join Date
    Feb 2009
    Location
    NJ, USA
    Posts
    476
    Thanks
    2
    Thanked 70 Times in 69 Posts
    It would definitely just have to do with the session scope. I think you just need:
    Code:
    <cfif isDefined( "session.started" ) AND session.started eq true>
      <!--- Logout button goes here --->
    <cfelse>
      <!--- Login form goes here --->
    </cfif>
    If that isn't it, let me know and I'll take a look tomorrow when I am more awake =P
    Last edited by Gjslick; 04-22-2009 at 06:12 AM. Reason: Changed the if statement a bit just incase session.started is not yet defined when this code is hit

  • #3
    New Coder
    Join Date
    Jul 2008
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Oh hurrah! Yes it works. Thanks!

    Now I need to make my logout more successful!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •