Skip to main content
Inspiring
January 4, 2011
Question

cfif and 'is user logged in'

  • January 4, 2011
  • 2 replies
  • 1015 views

Hi,

I'm pretty sure I'm doing this the wrong way, but currently, when someone goes on to my site and tries to do something such as post a comment on a blog, I have it say that they first need to create an account or log in, and that works.  Then when they log in, it allows them to post a comment and everything works fine... the problem, though, is that if they log out but don't close their browser, and they go to do something like post a comment while logged out, it allows them to do everything, except it doesn't say they are logged in as a user, it just shows up as:

Logged in as:

so it's blank like that, which is a problem because it posts as a no name user, which means there's no username provided on the site or in my database, so no one can check out the user's profile, follow the user or private message the user (all of those things work).

The cfif that I'm using to determine what to do if they are or aren't logged in looks like this:

                  <cfif isDefined("Session.MM_Username")>
                          <cfinclude template="userBlogsCommentFORM.cfm">
                  <cfelse>
                          <cfinclude template="notLoggedIn.cfm">
                  </cfif>

Thanks!

Don't know what I'd do without you people on this forum.

(you might need to see the user login/logout code, so i'll just post it below in case it's needed)

sidebar.cfm

-----------------------------------------------

<cfif isDefined("Session.MM_Username")>
    <cfinclude template="sideLoggedin.cfm">
<cfelse>
    <cfinclude template="sideLogin.cfm">
</cfif>

-----------------------------------------------

sideLoggedIn.cfm

-----------------------------------------------

<cfif IsDefined("URL.MM_logout") AND URL.MM_logout EQ "1">
  <cflock scope="Session" type="Exclusive" timeout="30" throwontimeout="no">
    <cfset Session.MM_Username="">
    <cfset Session.MM_UserAuthorization="">
  </cflock>
  <cfset MM_logoutRedirectPage="index.cfm">
  <cfif MM_logoutRedirectPage EQ "">
    <cfset MM_logoutRedirectPage=CGI.SCRIPT_NAME>
  </cfif>
  <cfset MM_logoutQuery=ListDeleteAt(CGI.QUERY_STRING,ListContainsNoCase(CGI.QUERY_STRING,"MM_logout=","&"),"&")>
  <cfif MM_logoutQuery NEQ "">
    <cfif Find("?",MM_logoutRedirectPage) EQ 0>
      <cfset MM_logoutRedirectPage=MM_logoutRedirectPage & "?" & MM_logoutQuery>
      <cfelse>
      <cfset MM_logoutRedirectPage=MM_logoutRedirectPage & "&" & MM_logoutQuery>
    </cfif>
  </cfif>
  <cflocation url="#MM_logoutRedirectPage#" addtoken="no">
</cfif>
<cfset CurrentPage=GetFileFromPath(GetBaseTemplatePath())>
<cfif Session.MM_Username IS "">
<cfinclude template="sideLogin.cfm">
<cfelse>


<cfparam name="SESSION.Session.MM_Username" default="1">
<cfparam name="SESSION.SESSION.Session.MM_Username" default="1">
<cfquery name="rsGetUserID" datasource="mymindsnotrighttest" username="mikewycklendt" password="Mex0Wix0">
SELECT user_id, username, password
FROM user_accounts
WHERE username = '<cfoutput>#Session.MM_Username#</cfoutput>'
</cfquery>
<style type="text/css">
<!--
.loggedInText {
    font-family: Tahoma, Geneva, sans-serif;
    font-size: 13px;
    color: #FFF;
}
-->
</style>
<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" align="center" valign="top" background="images/left_top.gif"><p class="left"><br />
                    <span class="loggedInText">Logged in as: <cfoutput>#Session.MM_Username#</cfoutput></span></p>
                    <p class="left"><a href="myProfile.cfm">Go to my Profile</a><br />
                      <br />
                    <a href="myProfile.cfm#readPMs">Private Messages<br />
                    </a><br />
                    <a href="followedUserPosts.cfm">Posts by Followed Users</a><br />
                    <br><a href="<cfoutput>#CurrentPage#?MM_logout=1</cfoutput>">Log out</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="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">TESTIMONIALS</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><a href="testamonials.cfm"></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="sevenThings.cfm">7 THINGS I'VE LEARNED</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="whendidyouknow.cfm">WHEN DID YOU KNOW...</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>

</cfif>

-----------------------------------------------

sideLogin.cfm

-----------------------------------------------

<cfset rsGetUserID.user_id="">
<cfif IsDefined("FORM.username")>
  <cfset MM_redirectLoginSuccess="index.cfm">
  <cfset MM_redirectLoginFailed="index.cfm">
  <cfquery  name="MM_rsUser" datasource="mymindsnotrighttest" username="mikewycklendt" password="Mex0Wix0">
  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>
<style type="text/css">
<!--

.SIBbg {
    background-attachment: fixed;
    background-image: url(images/stayinbalanceBGgif.gif);
    background-repeat: no-repeat;
    background-position: center top;
}
.blogcontent {
    font-size: 11pt;
    font-family: Tahoma, Geneva, sans-serif;
}

.comments {
    font-family: Tahoma, Geneva, sans-serif;
    font-size: 12px;
    font-weight: bold;
    text-decoration: underline;
    text-align: right;
    color: #246494;
}

.FeaturedBlogList {
    font-family: Tahoma, Geneva, sans-serif;
    font-size: 11px;
}

.FeaturedBlogListHeader {
    font-family: Tahoma, Geneva, sans-serif;
    font-size: 12px;
    font-weight: bold;
    color: #246494;
    /* [disabled]border-bottom-style: solid; */
}

.blogdate {
    font-size: 9pt;
    font-style: italic;
    font-family: Tahoma, Geneva, sans-serif;
}

.bloguserandwho {
    font-size: 10pt;
    font-style: normal;
    font-family: Tahoma, Geneva, sans-serif;
}
.redlink {
    font-family: Tahoma, Geneva, sans-serif;
    color: #F00;
    font-size: 10pt;
}

.blogtitle {
    font-size: 16px;
    font-weight: bold;
    font-family: Tahoma, Geneva, sans-serif;
    color: #246494;
}
.quoteStyle {
    font-family: Verdana, Geneva, sans-serif;
    font-size: 11pt;
    font-style: italic;
    color: #000;
}
.quoteOnTop {
    filter: DropShadow(Color=#246494, OffX=3, OffY=3, Positive=3);
}
.loginWhiteText {
    color: #FFF;
    font-family: Tahoma, Geneva, sans-serif;
    font-size: 11px;
}
.createAccount {
    font-family: Tahoma, Geneva, sans-serif;
    font-size: 13px;
    color: #FFF;
}
.createAccountLink {
    font-family: Tahoma, Geneva, sans-serif;
    font-size: 12pt;
    color: #FFF;
}
-->
</style>

<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" align="center" valign="top" background="images/left_top.gif"><form id="form1" name="form1" method="POST" action="<cfoutput>#MM_loginAction#</cfoutput>">
                    <table width="95%" border="0">
                        <tr>
                          <td> </td>
                          <td> </td>
                        </tr>
                        <tr>
                          <td class="loginWhiteText">username</td>
                          <td><input name="username" type="text" id="username" size="15" /></td>
                        </tr>
                        <tr>
                          <td class="loginWhiteText">password</td>
                          <td><input name="password" type="password" id="password" size="15" /></td>
                        </tr>
                        <tr>
                          <td> </td>
                          <td><input type="submit" name="submit" id="submit" value="Log in" /></td>
                        </tr>
                    </table>
                    <span class="createAccountLink"><a href="registerAccount.cfm" class="createAccountLink">Create Account</a><br />
                    </span><br />
                  </form></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">TESTIMONIALS</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><p class="menu"><img src="images/e01.gif" alt="" width="14" height="13" border="0" align="absmiddle" />  <a href="sevenThings.cfm">7 THINGS I'VE LEARNED</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="whendidyouknow.cfm">WHEN DID YOU KNOW...</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>

    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    January 4, 2011

    Just like Dan, I looked for the logout code. I expected to see something like

    <cfif IsDefined("URL.MM_logout") AND URL.MM_logout EQ "1">

    <cflogout>

    ...

    etc., etc.

    Inspiring
    January 5, 2011

    Hi,

    Here the problem is, you are just checking whether the session variable is defined;

    and not checking for the length of that; like len(#session.username#) NEQ 0.

    Because when the user logs out, you are making the session.username =""; so this makes the variable as null, but it wi ll be defined all the times.

    Another option is you could clear the session using STRUCTCLEAR();

    <cfif isDefined("Session.MM_Username") AND len(#Session.MM_Username#) NEQ 0>
        <cfinclude template="sideLoggedin.cfm">
    <cfelse>
        <cfinclude template="sideLogin.cfm">
    </cfif>

    Please try this out..

    Inspiring
    January 4, 2011

    Forget all that display code.  Where is the code that runs when the user logs out?