Skip to main content
Inspiring
January 8, 2009
Question

cfapplication in Application.cfc

  • January 8, 2009
  • 7 replies
  • 2843 views
I've been working on this for hours and from what I read this should be a simple thing. I want my users to be able to login and if they aren't I want the login webpage to display so they can. My login page is called loginform.cfm.

In my Application.cfc file I have the following:
<!--- activate session variables for login--->
<cfapplication name="guiDb"
sessionmanagement="yes">

When I try to run loginform.cfm I get the following error:
The system has attempted to use an undefined value, which usually indicates a programming error, either in your code or some system code.

Null Pointers are another name for undefined values.

The error occurred in /usr/local/www/taxw/taxweb/gui/Application.cfc: line 45

43 : <!--- activate session variables for login--->
44 : <cfapplication name="guiDb"
45 : sessionmanagement="yes">

Can someone tell me what's wrong? Thanks

    This topic has been closed for replies.

    7 replies

    Participating Frequently
    January 12, 2009
    Well, it just complicates things a bit.

    Make sure the application names match (in the Application.cfm and the Application.cfc). Add SESSIONMANAGEMENT="Yes" to your Application.cfm <cfapplication...> tag.

    Other than that, you can use all the code I just posted. If you're using the same APPLICATION.__ variables in your gui folder, you don't need an OnApplicationStart method. Just make sure the names are the same.

    Did you understand what I was saying about APPLICATION versus SESSION variables for user-specific stuff?
    Participating Frequently
    January 12, 2009
    Here's a very simple starting point. Folder structure is:
    /Application.cfc
    /index.cfm
    /loginform.cfm
    /authuser.cfm

    Any requests to any .cfm page will check to see if the user is logged in. If not, they will be presented with loginform.cfm.

    All the code to authenticate them and setup their session is in authuser.cfm.

    =======Application.cfc=================
    <cfcomponent displayname="Application" output="false" hint="Handle the application">
    <!--- Set up the application. --->
    <cfset THIS.Name = "TestAuth" />
    <cfset THIS.SessionManagement = true />

    <!--- Run when application starts up --->
    <cffunction name="onApplicationStart" returnType="boolean" output="false" hint="Fires when the application is first created.">
    <cfset APPLICATION.DSN = "MyDatasource">
    <cfreturn true />
    </cffunction>


    <!--- Run before the request is processed --->
    <cffunction name="onRequestStart" returnType="boolean" output="false" hint="Fires at first part of page processing.">
    <cfargument name="thePage" type="string" required="true">

    <cfinclude template="AuthUser.cfm">

    <cfreturn true>
    </cffunction>
    </cfcomponent>

    ========index.cfm========================
    <html><body><cfoutput>Hi #SESSION.User.FirstName# #SESSION.User.LastName#! Thanks for being logged in.</cfoutput></body></html>

    ========loginform.cfm=====================
    <html><body>
    <cfif isDefined("Message")><cfoutput>#Message#</cfoutput><br/></cfif>
    <form name="LoginForm" action="/" method="post">
    Username:<input type="text" name="UserLogin" size="30" maxsize="30"><br/>
    Password:<input type="password" name="UserPassword" size="30" maxsize="30"><br/>
    <input type="submit" name="Login" value="Login">
    </form></body></html>

    =========authuser.cfm======================
    <cflogin>
    <cfif not (isDefined("form.userlogin") and isdefined("form.userpassword"))>
    <cfinclude template="loginform.cfm">
    <cfabort>
    <cfelseif form.userlogin eq "" or form.userpassword eq "">
    <cfset MESSAGE="Username and Password are both required.">
    <cfinclude template="loginform.cfm">
    <cfabort>
    <cfelse>
    <!--- This login query is case-insensitive for username, but case-sensitive for password --->
    <cfquery name="authUser" datasource="#Application.DSN#">
    select userid,username from userauth
    where lower(username) = lower(<cfqueryparam value="#form.userlogin#" cfsqltype="CF_SQL_VARCHAR">) and
    pword = <cfqueryparam value="#form.userpassword#" cfsqltype="CF_SQL_VARCHAR">
    </cfquery>

    <cfif authUser.recordcount eq 1>
    <cfloginuser name="#authuser.userid#" password="#form.userpassword#" roles="USER">

    <cfquery name="getUser" datasource="#Application.DSN#">
    select * from users where userid = <cfqueryparam value="#authuser.userid#" cfsqltype="CF_SQL_INTEGER">
    </cfquery>

    <cfset SESSION.User = StructNew()>
    <cfset SESSION.User.UserID = getuser.userid>
    <cfset SESSION.User.UserName = form.userlogin>
    <cfset SESSION.User.RemoteAddress = cgi.REMOTE_ADDR>
    <cfset SESSION.User.FirstName = getuser.firstname>
    <cfset SESSION.User.LastName = getuser.lastname>

    <cfelse>
    <cfset MESSAGE="The login information you entered is invalid.">
    <cfinclude template="loginform.cfm">
    <cfabort>
    </cfif>
    </cfif>
    </cflogin>
    tgooldAuthor
    Inspiring
    January 12, 2009
    Thanks for your help. This is going to take a while for me to get this done because I want to go slowly and look up each line if I don't understand. I want to completely understand what's going on so I can at least learn for the next time. Thanks again.
    tgooldAuthor
    Inspiring
    January 9, 2009
    Thanks Kronin555 for your help. I'm ready to get off work now so I'll post the information on Monday. I work for a state agency and as you probably know, they don't have money right now for training of any kind. We've had to learn CF on our own, as many people do. I have Ben Forta's book on CF and it's been really great, but I just can't seem to get this piece of the puzzle figured out. Thanks again.
    Inspiring
    January 8, 2009
    > Thanks so much. Now, do I have to put any code in order for this to work on
    > each of my web pages? I saw something about using an IsDefined statement
    > somewhere at the top of each page. If I do that, what do I use as the variable
    > for IsDefined?

    I thikn you could probably benefit from reading the relevant documentation,
    as it's all spelled out fairly clearly:

    http://livedocs.adobe.com/coldfusion/8/htmldocs/appFramework_03.html

    Post back if anything doesn't make sense, but your current questions will
    be answered by reading the docs.

    --
    Adam
    tgooldAuthor
    Inspiring
    January 8, 2009
    This still isn't working for me. I don't believe I'm supposed to be posting newbie questions here according to Adam. Is there a place I can do that? I'm not able to find what I need in the docs.
    Participating Frequently
    January 8, 2009
    This place is fine for your question.

    Define "not working". What's happening? Be as detailed as possible.

    Post your Application.cfc and your folder structure, showing where all your pages (including Application.cfc) are stored. Also include any other Application.cfc or Application.cfm files.
    Participating Frequently
    January 8, 2009
    Easiest way: make sure that Application.cfc is in the root folder for this website. Don't have any other Application.cfc/Application.cfm files anywhere in your website.

    If you do this, then you have code in your Application.cfc that ensures they're logged in. No modification necessary to all your other files.
    tgooldAuthor
    Inspiring
    January 8, 2009
    Thanks so much. Now, do I have to put any code in order for this to work on each of my web pages? I saw something about using an IsDefined statement somewhere at the top of each page. If I do that, what do I use as the variable for IsDefined?
    Participating Frequently
    January 8, 2009
    You don't use the <cfapplication> tag in Application.cfc. Simply do this, inside your <cfcomponent..>
    <cfset THIS.SessionManagement = true />