Skip to main content
Inspiring
July 22, 2010
Answered

logging in a user

  • July 22, 2010
  • 2 replies
  • 1749 views

Hi, I posted the other day and was told to read up on different scopes of variables and felt stupid because it was so simple.

So I figured out how to log in users and do what I want to do, but I have one problem.

My website has a sidebar which contains the login form.  The sidebar is an include used for the whole site.  When I set everything up to log in a user, and test it, when i'm previewing sideLogin.cfm on its own, it does what it should.  I preview sideLogin.cfm and it works, then goes to sideLoggedin.cfm and shows that the user is logged in. All that sidebar.cfm has is:

<cfif IsDefined("Session.Username")>

      <cfinclude template="sideLoggedin.cfm">

<cfelse>

      <cfinclude template="sideLogin.cfm">

</cfif>

Also, when I log in using nothing but sideLogin.cfm which goes to sideLoggedIn.cfm, sideLoggedin.cfm shows the user is logged in, but then when I go to index.cfm (or any other page), in the same session, I see sideLogin.cfm even though I know that the session variable has been set.

I should also mention that I did most of this stuff in Dreamweaver CS4 and applied the user authentication server behavior to the form--not Coldfusion code.

    This topic has been closed for replies.
    Correct answer JR__Bob__Dobbs

    Thank you so much for doing your best to help me out this week.  Below is the code I'm working with.  I might as well ask now, because I think it would go along with this same code.  What I have give me session variables for username and password.  In my users table of my database, I have a user_id for each user.  Right now, I can get the user id for the logged in user, but the way I can do it right now involves me putting an extra binding on each individual page that filters the table to get the user_id by matching the session variable to the stored username in the table... there's got to be a simpler way, I would think, and I have a feeling that simpler way woudl involve the code below.

    This is all the code for each part of what I'm trying to put together:

    ===========================================================
    sideLogin.cfm:
    ===========================================================

    <cfif IsDefined("FORM.username")>
      <cfset MM_redirectLoginSuccess="index.cfm">
      <cfset MM_redirectLoginFailed="index.cfm">
      <cfquery  name="MM_rsUser" datasource="mymindsnotrighttest">
      SELECT username,password FROM user_accounts WHERE username=<cfqueryparam value="#FORM.username#" cfsqltype="cf_sql_clob" maxlength="65535"> AND password=<cfqueryparam value="#FORM.password#" cfsqltype="cf_sql_clob" maxlength="65535">
      </cfquery>
      <cfif MM_rsUser.RecordCount NEQ 0>
        <cftry>
          <cflock scope="Session" timeout="30" type="Exclusive">
            <cfset Session.MM_Username=FORM.username>
            <cfset Session.MM_UserAuthorization="">
          </cflock>
          <cfif IsDefined("URL.accessdenied") AND false>
            <cfset MM_redirectLoginSuccess=URL.accessdenied>
          </cfif>
          <cflocation url="#MM_redirectLoginSuccess#" addtoken="no">
          <cfcatch type="Lock">
            <!--- code for handling timeout of cflock --->
          </cfcatch>
        </cftry>
      </cfif>
      <cflocation url="#MM_redirectLoginFailed#" addtoken="no">
      <cfelse>
      <cfset MM_LoginAction=CGI.SCRIPT_NAME>
      <cfif CGI.QUERY_STRING NEQ "">
        <cfset MM_LoginAction=MM_LoginAction & "?" & XMLFormat(CGI.QUERY_STRING)>
      </cfif>
    </cfif>
    <table border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="188" valign="top" bgcolor="#246494">
                <!-- left -->
                <table border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td height="36" background="images/left_top.gif" valign="top"><img src="images/menu.jpg" width="188" height="36"></td>
                  </tr>
                  <tr>
              
                      <td width="188" height="161" background="images/left_top.gif" valign="top"><p> </p>
                        <form id="form1" name="form1" method="POST" action="<cfoutput>#MM_loginAction#</cfoutput>">
                  <label>username
                            <input name="username" type="text" id="username" size="15" />
                          </label>
                          <br />
                          <label>password
                            <input name="password" type="text" id="password" size="15" />
                          </label>
                          <br />
                          <label>
                            <input type="submit" name="submit" id="submit" value="Submit" />
                        </label>
                        </form>
                      <p class="left">  </p></td>
                  
                  </tr>
                  <tr>
                    <td bgcolor="#48C8FF"><img src="images/px1.gif" width="1" height="1" alt="" border="0"></td>
                  </tr>
                  <tr>
                    <td>
                      <p class="menu"><img src="images/e01.gif" alt="" width="14" height="13" border="0" align="absmiddle"> <a href="allBlogArchives.cfm">BLOG ARCHIVE</a></p>
                    </td>
                  </tr>
                  <tr>
                    <td bgcolor="#48C8FF"><img src="images/px1.gif" width="1" height="1" alt="" border="0"></td>
                  </tr>
                  <tr>
                    <td>
                      <p class="menu"><img src="images/e01.gif" alt="" width="14" height="13" border="0" align="absmiddle"> <a href="supportGroups.cfm">SUPPORT GROUPS</a></p>
                    </td>
                  </tr>
                  <tr>
                    <td bgcolor="#48C8FF"><img src="images/px1.gif" width="1" height="1" alt="" border="0"></td>
                  </tr>
                  <tr>
                    <td>
                      <p class="menu"><img src="images/e01.gif" alt="" width="14" height="13" border="0" align="absmiddle"> <a href="doctorqanda.cfm">DOCTORS WORDS</a></p>
                    </td>
                  </tr>
                  <tr>
                    <td bgcolor="#48C8FF"><img src="images/px1.gif" width="1" height="1" alt="" border="0"></td>
                  </tr>
                  <tr>
                    <td>
                      <p class="menu"><img src="images/e01.gif" alt="" width="14" height="13" border="0" align="absmiddle">  <a href="testamonials.cfm">TESTAMONIALS</a></p>
                    </td>
                  </tr>
                  <tr>
                    <td bgcolor="#48C8FF"><img src="images/px1.gif" width="1" height="1" alt="" border="0"></td>
                  </tr>
                  <tr>
                    <td>
                      <p class="menu"><img src="images/e01.gif" alt="" width="14" height="13" border="0" align="absmiddle">  <a href="littleVictories.cfm">LITTLE VICTORIES</a></p>
                    </td>
                  </tr>
                  <tr>
                    <td bgcolor="#48C8FF"><img src="images/px1.gif" width="1" height="1" alt="" border="0"></td>
                  </tr>
                  <tr>
                    <td height="24"><p class="menu"><img src="images/e01.gif" alt="" width="14" height="13" border="0" align="absmiddle">  <a href="lyricsMusicPoetry.cfm">LYRICS / MUSIC / POETRY</a></p></td>
                  </tr>
                  <tr>
                    <td bgcolor="#48C8FF"><img src="images/px1.gif" width="1" height="1" alt="" border="0" /></td>
                  </tr>
                  <tr>
                    <td><p class="menu"><img src="images/e01.gif" alt="" width="14" height="13" border="0" align="absmiddle" /> <a href="books.cfm"> BOOKS</a></p></td>
                  </tr>
                  <tr>
                    <td bgcolor="#48C8FF"><img src="images/px1.gif" width="1" height="1" alt="" border="0" /></td>
                  </tr>
                  <tr>
                    <td><p class="menu"><img src="images/e01.gif" alt="" width="14" height="13" border="0" align="absmiddle" />  <a href="findUsers.cfm">FIND USERS</a></p></td>
                  </tr>
                  <tr>
                    <td bgcolor="#48C8FF"><img src="images/px1.gif" width="1" height="1" alt="" border="0" /></td>
                  </tr>
                  <tr>
                    <td><p class="menu"><img src="images/e01.gif" alt="" width="14" height="13" border="0" align="absmiddle" />  <a href="chatroom.cfm">CHATROOM</a></p></td>
                  </tr>
                  <tr>
                    <td height="37" bgcolor="#246494"> </td>
                  </tr>
    </table>


    ===================================================================
    sideLoggedIn.cfm
    ===================================================================

    <table border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="188" valign="top" bgcolor="#246494">
                <!-- left -->
                <table border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td height="36" background="images/left_top.gif" valign="top"><img src="images/menu.jpg" width="188" height="36"></td>
                  </tr>
                  <tr>
              
                      <td width="188" height="161" background="images/left_top.gif" valign="top"><p class="left"><br />
                        Logged in as: <cfoutput>#Session.MM_Username#</cfoutput></p>
                        <p class="left">Go to my Profile<br />
                          Private Messages: ( )
                    </p></td>
                  
                  </tr>
                  <tr>
                    <td bgcolor="#48C8FF"><img src="images/px1.gif" width="1" height="1" alt="" border="0"></td>
                  </tr>
                  <tr>
                    <td>
                      <p class="menu"><img src="images/e01.gif" alt="" width="14" height="13" border="0" align="absmiddle"> <a href="allBlogArchives.cfm">BLOG ARCHIVE</a></p>
                    </td>
                  </tr>
                  <tr>
                    <td bgcolor="#48C8FF"><img src="images/px1.gif" width="1" height="1" alt="" border="0"></td>
                  </tr>
                  <tr>
                    <td>
                      <p class="menu"><img src="images/e01.gif" alt="" width="14" height="13" border="0" align="absmiddle"> <a href="supportGroups.cfm">SUPPORT GROUPS</a></p>
                    </td>
                  </tr>
                  <tr>
                    <td bgcolor="#48C8FF"><img src="images/px1.gif" width="1" height="1" alt="" border="0"></td>
                  </tr>
                  <tr>
                    <td>
                      <p class="menu"><img src="images/e01.gif" alt="" width="14" height="13" border="0" align="absmiddle"> <a href="doctorqanda.cfm">DOCTORS WORDS</a></p>
                    </td>
                  </tr>
                  <tr>
                    <td bgcolor="#48C8FF"><img src="images/px1.gif" width="1" height="1" alt="" border="0"></td>
                  </tr>
                  <tr>
                    <td>
                      <p class="menu"><img src="images/e01.gif" alt="" width="14" height="13" border="0" align="absmiddle">  <a href="testamonials.cfm">TESTAMONIALS</a></p>
                    </td>
                  </tr>
                  <tr>
                    <td bgcolor="#48C8FF"><img src="images/px1.gif" width="1" height="1" alt="" border="0"></td>
                  </tr>
                  <tr>
                    <td>
                      <p class="menu"><img src="images/e01.gif" alt="" width="14" height="13" border="0" align="absmiddle">  <a href="littleVictories.cfm">LITTLE VICTORIES</a></p>
                    </td>
                  </tr>
                  <tr>
                    <td bgcolor="#48C8FF"><img src="images/px1.gif" width="1" height="1" alt="" border="0"></td>
                  </tr>
                  <tr>
                    <td height="24"><p class="menu"><img src="images/e01.gif" alt="" width="14" height="13" border="0" align="absmiddle">  <a href="lyricsMusicPoetry.cfm">LYRICS / MUSIC / POETRY</a></p></td>
                  </tr>
                  <tr>
                    <td bgcolor="#48C8FF"><img src="images/px1.gif" width="1" height="1" alt="" border="0" /></td>
                  </tr>
                  <tr>
                    <td><p class="menu"><img src="images/e01.gif" alt="" width="14" height="13" border="0" align="absmiddle" /> <a href="books.cfm"> BOOKS</a></p></td>
                  </tr>
                  <tr>
                    <td bgcolor="#48C8FF"><img src="images/px1.gif" width="1" height="1" alt="" border="0" /></td>
                  </tr>
                  <tr>
                    <td><p class="menu"><img src="images/e01.gif" alt="" width="14" height="13" border="0" align="absmiddle" />  <a href="findUsers.cfm">FIND USERS</a></p></td>
                  </tr>
                  <tr>
                    <td bgcolor="#48C8FF"><img src="images/px1.gif" width="1" height="1" alt="" border="0" /></td>
                  </tr>
                  <tr>
                    <td><p class="menu"><img src="images/e01.gif" alt="" width="14" height="13" border="0" align="absmiddle" />  <a href="chatroom.cfm">CHATROOM</a></p></td>
                  </tr>
                  <tr>
                    <td height="37" bgcolor="#246494"> </td>
                  </tr>
    </table>

    ==========================================================================
    sideBar.cfm
    ==========================================================================

    <cfif IsDefined("Session.Username")>

          <cfinclude template="sideLoggedin.cfm">

    <cfelse>

          <cfinclude template="sideLogin.cfm">

    </cfif>

    ===================================================================
    Application.cfc
    ====================================================================

    <cfcomponent>
    <cfscript>
            this.name = "MyMindsNotRight";
            this.sessionmanagement = true;
        </cfscript>

    </cfcomponent>

    ================================================================
    index.cfm
    ===============================================================

    calls sideBar.cfm -- <cfinclude template="sideBar.cfm -- placed in the spot the sidebar is supposed to show up.


    Your login code in sideLogin.cfm sets a session variable named "Session.MM_Username". Your sideBar.cfm code checks for a variable named "Session.Username". The variable names in the two files do not match.

    2 replies

    Inspiring
    July 22, 2010

    Another item to check.

    Where is the code snippet you posted being executed?  Some application events don't have access to the session scope.

    http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7d39.html

    Inspiring
    July 22, 2010

    Things to check

    1. You might try using CFDUMP to output your session variables.

    <cfif IsDefined("Session.Username")>

          <cfinclude template="sideLoggedin.cfm">

    <cfelse>

         <cfdump var="#session#" label="Session Variables" />

          <cfinclude template="sideLogin.cfm">

    </cfif>

    2. Is sideLogin.cfm included by any other CF files?

    wycksAuthor
    Inspiring
    July 22, 2010

    1) I go to index.cfm which is showing sideLogin.cfm.  I put in the username and password and it stays on index.cfm and the cfdump shows the logged in username, but sideLogin.cfm still shows on index.cfm

    2) sideLogin.cfm and sideLoggedin.cfm aren't directly included on any page--the include that's on every page is sidebar.cfm which contains nothing but the cfif code I posted above.

    Inspiring
    July 22, 2010

    How is sidebar.cfm included? It is an CFINCLUDE on index.cfm or is it part of your Application.cfm or Application.cfc code?