Skip to main content
Inspiring
November 16, 2011
Question

Problem with <cfif> tag

  • November 16, 2011
  • 4 replies
  • 5887 views

Basically, I'm trying to add some text to my header depending on whether or not the user is logged in. If they are, it will display one thing (Welcome, My Account, Logout), if they are not, it will display something else (Login/Register). I'm using Dreamweaver which I know can do some silly things with code, but being a CF and web design novice, the only way I could think of to do this is with a <cfif> tag. Here is what I've come up with, but I keep getting errors and I cannot figure out why. The first chunk of code is for the logout query, the second is a recordset to grab the first name of the current user, if they are logged in. Those appear to be working okay, the issues seem to be coming from the <cfif> tag in the <body>. The error I'm getting says I need a closing </cfif> tag, but it looks to me like it should be okay. Any thoughts on what I screwed up?

Thanks for any help you can give!

Jeremy

<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 IsNull(Session.MM_Username) EQ false>

  <cfquery name="userinfo" datasource="everettweb">

    SELECT Users.[First Name]

    FROM Users

    WHERE Users.Email = Session.MM_Username

    </cfquery>

</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>

<link href="../main.css" rel="stylesheet" type="text/css" />

<style type="text/css">

<!--

a.headerlink:link {color: #FFFFFF;}

a.headerlink:hover {color: #D7A05D;}

a.headerlink:visited {color: #FFFFFF;}

a.headerlink:active {color: #FFFFFF;}

-->

</style>

</head>

<body>

<cfif IsNull(Session.MM_Username) EQ false>

  <p class="loginstatus"><cfoutput query="userinfo">Welcome, #[First Name]#!</cfoutput> <a href="../register_edit.cfm" class="headerlink">My Account</a> / <a href="<cfoutput>#CurrentPage#?MM_logout=1</cfoutput>">Logout</a><img src="../images/clear.gif" width="40" height="20" border="0" /></p>

<cfelse>

  <p class="loginstatus"><a href="../login.cfm" class="headerlink">Login</a> / <a href="../register.cfm" class="headerlink">Register</a><img src="../images/clear.gif" width="40" height="20" border="0" /></p>

</cfif>

</body>

</html>

    This topic has been closed for replies.

    4 replies

    jawmusicAuthor
    Inspiring
    November 18, 2011

    Thanks everyone for your help. I was able to get it to work as I had hoped with a little trial and error.

    BKBK
    Community Expert
    Community Expert
    November 18, 2011

    jawmusic wrote:

    Thanks everyone for your help. I was able to get it to work as I had hoped with a little trial and error.

    Good for you. Then please mark your question as answered.

    BKBK
    Community Expert
    Community Expert
    November 17, 2011

    I have a number of suggestions for you.

    1) Remove spaces from database column names. Thus, in place of 'first name', use first_name or firstName.

    2) The query will then be something like

    SELECT Users.first_name

    FROM Users

    WHERE Users.Email = '#Session.MM_Username#'

    3) Also change accordingly,

    <cfoutput query="userinfo">Welcome, #first_name#!</cfoutput>

    4) If you cannot change the name of the column, or don't want to, then delimit the column name with the backtick or ASCII 96 character

    SELECT Users.`first name` as first_name

    FROM Users

    WHERE Users.Email = '#Session.MM_Username#'

    or, alternatively,

    SELECT Users.#chr(96)#first name#chr(96)# as first_name

    FROM Users

    WHERE Users.Email = '#Session.MM_Username#'

    Inspiring
    November 16, 2011

    So the error message says that you need a closing <cfif> tag?  Given how many if/else blocks you have, it would be hard to solve by simply eyeballing the code.

    I suggest adding some whitespace to your code to make it more readable.  Then, comment out everything starting with this line:

    <cfset CurrentPage=GetFileFromPath(GetBaseTemplatePath())>

    then run the page.  If it runs without error, start uncommenting blocks of code until it crashes.  That will help you identify the problem.

    Inspiring
    November 16, 2011

    The most impiortant thing when posting here for help and you "keep getting errors", is to include what the error message is, as well as looking at the line of code the error points to.  And paying attention to what line the error claims to be on... you should be able to cut down how much code you need to post (or, indeed, spot what the problem is yourself).

    --

    Adam

    jawmusicAuthor
    Inspiring
    November 16, 2011

    My apologies if my post was unclear. As I said, the error points to the <cfif> tag in the body and says it needs an end tag. This is line 45.  The exact wording of the error is:

    Context validation error for the cfif tag.

    The start tag must have a matching end tag. An explicit end tag can be provided by adding </cfif>. If the body of the tag is empty, you can use the shortcut <cfif .../>.

    The CFML compiler was processing:

    • The body of a cfoutput tag beginning on line 46, column 27.

    The error occurred in C:\ColdFusion9\wwwroot\Everett2\Templates\login_status.cfm: line 45

    43 :

    44 : <Body>

    45 : <cfif IsNull(Session.MM_Username) EQ false>

    46 : <p class="loginstatus"><cfoutput query="userinfo">Welcome, #[First Name]#!</cfoutput> <a href="../register_edit.cfm" class="headerlink">My Account</a> / <a href="<cfoutput>#CurrentPage#?MM_logout=1</cfoutput>">Logout</a><img src="../images/clear.gif" width="40" height="20" border="0" /></p>

    47 : <cfelse>

    I included the additional code for a couple reasons. First, I thought maybe a more experienced coder could give me a better way to do this, if one exists, and second, because I know sometimes the error isn't actually in the line the message references, but rather in the syntax of the function above. Thanks for your help!

    Jeremy

    Inspiring
    November 16, 2011

    Honestly there really are no shortcuts to figuring out that type of error, which is why it is good to test in small blocks.  Like others suggested, start by commenting out the code within that last set of cfif/cfelse tags.  (It is only a few lines) If it works with an empty cfif/cfelse block,  the problem is not a missing tag.  It is the code within the cfif or cfelse block.  Uncomment one line at a time until you find the offending party. 

    -Leigh