Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

logging in a user

Explorer ,
Jul 22, 2010 Jul 22, 2010

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.

1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Jul 24, 2010 Jul 24, 2010

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.

Translate
Enthusiast ,
Jul 22, 2010 Jul 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 22, 2010 Jul 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 22, 2010 Jul 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 22, 2010 Jul 22, 2010

its <cfinclude template="sidebar.cfm"> on all of my pages.

my Application.cfc doesn't have much:

<cfcomponent>

<cfscript>

      this.name = "mmnr";

      this.sessionmanagement = true;

</cfscript>

</cfcomponent>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 22, 2010 Jul 22, 2010

If you want to process this type of logic on all your pages, the onRequestStart() function is where you want to put your code.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 22, 2010 Jul 22, 2010

which part of the code, specifically?  from what i understand, onRequestStart() executes before each page request and goes in the Application.cfc file.  If I put my code in there, how will it know where the sidebar.cfm file is supposed to go in the page?

Sorry for being a dumb beginner who is learning as he goes.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 22, 2010 Jul 22, 2010

Why don't you put the sidebar in the same place on every page?


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 23, 2010 Jul 23, 2010

"Why don't you put the sidebar in the same place on every page?"

it does go in the same place on every page, and right now I have the <cfinclude template="sidebar.cfm"> in the place where it goes, but if I move it to the Application.cfc file, how will it know where its supposed to go?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 23, 2010 Jul 23, 2010

In order to troubleshoot it would be useful to see more of your code. Given only the snippet you have posted it is not easy to determine what the root of your trouble is.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 23, 2010 Jul 23, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 24, 2010 Jul 24, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 25, 2010 Jul 25, 2010
LATEST

Thank you so much for spotting my mistake... i actually felt much better about my comprehension of ColdFusion for a second because the problem was something so simple...

...until I hit my next road block.  I'm pretty sure that the problem here is in the Application.cfc code -- I'm trying to get it to figure out what the user_id is for the user who logs in so I can use the user's user_id variable on all my pages.

===================================================
Application.cfc
===================================================
<cfcomponent output="false">
<cfscript>
        this.name = "MyMindsNotRight";
        this.sessionmanagement = true;
    </cfscript>

<cffunction name="OnRequestStart" returntype="boolean" output="true">
    <cfset REQUEST.dataSource = "mymindsnotrighttest">
   
    <cfif isDefined("Session.MM_Username")>
    <cfquery name="userID"
        datasource="mymindsnotrighttest"
        result="user_ID">
            SELECT username, user_id
            FROM user_accounts
            WHERE username=#Session.MM_Username#
     </cfquery>
</cfif>
   
    <cfreturn true>
   
    </cffunction>

</cfcomponent>

=======================================================
adminLogIn.cfm
=======================================================
<cfif IsDefined("FORM.username")>
  <cfset MM_redirectLoginSuccess="admin.cfm">
  <cfset MM_redirectLoginFailed="adminLogIn.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>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
  <form id="form1" name="form1" method="POST" action="<cfoutput>#MM_loginAction#</cfoutput>">
    <table width="95%" border="0">
      <tr>
        <td width="11%">username</td>
        <td width="89%"><label>
          <input type="text" name="username" id="username" />
        </label></td>
      </tr>
      <tr>
        <td>password</td>
        <td><label>
          <input type="text" name="password" id="password" />
        </label></td>
      </tr>
      <tr>
        <td> </td>
        <td><label>
          <input type="submit" name="submit" id="submit" value="Log In" />
        </label></td>
      </tr>
    </table>
  </form>
  <p> </p>
</body>
</html>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 22, 2010 Jul 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources